0

I am making a jsonp ajax call to a local service which responds with a valid javascript response. The ajax request looks like below:

$.ajax({
        url: "http://localhost:12345",
        type: "GET",
        dataType: "jsonp",
        jsonpCallback: "myFunction",
        cache: false,
        success: function (data) {
            console.log('success');
        },
        error: function (jqxhrInternal, message, errorThrown) {
            console.log(JSON.stringify(jqxhrInternal));
            console.log('message: ' + message);
            console.log('errorThrown: ' + errorThrown);
        }
    });

And the response returned has a "application/javascript" Content-Type header with a value of:

myFunction({"key": "value"});

The script is added to the webpage automatically and the function is executed without any errors, however the ajax function is entering the error function and is telling me that myFunction was not called. I've googled for hours and tried everything, jQuery just won't detect that the function has successfully executed. The jQuery version is 3.6.1. What is the reason for this behaviour from jQuery and how can I resolve it?

4
  • You defined the jsonpCallback: "myFunction" in ajax request. So when ajax get the response, it will call myFunction with the result too. You need to write function myFunction(data){} or remove the jsonpCallback in ajax request. Commented Jun 5, 2024 at 4:12
  • Agreed with @user25009716 here, either remove jsonpCallback: "myFunction", or create a function function myFunction(data){ //...do your desired stuff here...} Commented Jun 5, 2024 at 6:34
  • What is the exact error message? Commented Jun 5, 2024 at 15:51
  • @Death-is-the-real-truth The function already exists globally, and it is successfully executed when the ajax call returns, as I can see the changes it makes on the webpage, despite that it still shows a parsererror and that myFunction was not called (which it obviously was) Commented Jun 6, 2024 at 0:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.