AJAX Call with to_pdf=1 From Custom Form Returns Menu HTML and JavaScript

I’m relatively new to SuiteCRM. When I do an AJAX call using jQuery from a custom form and a custom entry point I get the JSON I ask for, but it also returns a bunch of HTML and JS, causing an error. I’ll show code below and appreciate any help. Thanks!

SuiteCRM 7.13

JavaScript / jQuery

$.ajax({
                method: "POST",
                url: '/index.php?entryPoint=FindContacts&module=My_Module&to_pdf=1',
                data: {my_data: myData}
            }).done(function(data, textStatus, jqXHR) {
                console.log(data);
            });

I set up my entry point according to the documentation:

SuiteCRM Entry Points

This is what is returned. The JSON is what my entry point is returning, and the rest is the undesirable HTML / JS.

{"message":"No results."}  <-- MY JSON

A SNIPPET OF THE OTHER DATA RETURNED (that I thought was suppressed with to_pdf=1)
<div class="clear"></div>
<div class='listViewBody'>
    <script type="text/javascript" src="include/javascript/popup_parent_helper.js?v=-p6kmkC1-L0iwsb5UaV6Tg"></script>
    <script></script>
        
        <script>


            function addXMLRequestCallback(callback) {
                var oldSend, i;
                if (XMLHttpRequest.callbacks) {
                    // we've already overridden send() so just add the callback
                    XMLHttpRequest.callbacks.push(callback);
                } else {
                    // create a callback queue
                    XMLHttpRequest.callbacks = [callback];
                    // store the native send()
                    oldSend = XMLHttpRequest.prototype.send;
                    // override the native send()
                    XMLHttpRequest.prototype.send = function () {
                        // process the callback queue
                        for (i = XMLHttpRequest.callbacks.length - 1; i >= 0; i--) {
                            XMLHttpRequest.callbacks[i](this);
                        }
                        // call the native send()
                        oldSend.apply(this, arguments);
                    }
                }
            }

            function refreshSearchForm() {
                $('.search_form textarea').each(function (i, e) {
                    $(e).css('max-width', $(e).parent().width());
                });

Thanks for looking, and any help! Have a good day!!!

Do you have an after_ui hooks in your system? Possibly from some add-on you installed or other custom code?

Thank you so much for leading me in the correct direction. Yes, there are two modules that use after_ui hooks in custom/modules/logic_hooks.php. I experimented a little with removing them. But in the end decided it was not a good idea to do so. I also looked into the /include/utils.php - remove_logic_hook function, but after tracing the code, I decided to not go that route due to various reasons.

So, while not ideal, all I did was:

header('Content-Type: application/json; charset=utf-8');
echo json_encode($json);
exit;

The exit statement stopped the execution chain. I don’t really like doing things this way, but for now it works without errors.

Thanks again! I really appreciate your help.

In the back-end (PHP controller) there are ways to say that your web request is just an Ajax style thing, not expecting a full view, to keep SuiteCRM from sending in extra stuff.

I think it’s something like setting $this->view = ''; on the controller, but I am not sure and I can’t check right now.

You might find it in some tutorial about using Ajax calls in SuiteCRM.