0

I have the following javascript:

jQuery(document).ready( function($) {

    var id = "123";
    var api = "example.com:8999/".concat(id)

    $.ajax({
            url : api,
            type: 'GET',
            dataType: 'jsonp',
            // jsonpCallback: "localcallback",
            success: function (data) { alert('success'); }
    })
});

I can see the response in chrome dev tools, but the alert isn't getting called. Ultimately I need to work with this response to set the value of a div.

Image of chrome tools:

Response

Thanks

EDIT: Put 'POST', was using 'GET', still not working. Also, I think I'd prefer "mom and pop" json, but due to CORS and the fact I'm not good with the web and am just trying to hack this together.

tshauck
  • 20,746
  • 8
  • 36
  • 36

2 Answers2

0

Your server is not returning JSONP. It's returning plain JSON. If you specify JSONP, then the server must explicitly create a JSONP formatted response or the ajax response will not be received and processed properly.

FYI, a JSONP request is sent via a script tag (that's how it gets around the same-origin limitation for cross domain requests) and the response has to be formatted as a script that calls a function and passed the requested data to that function. You can read about how JSONP works here.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Just make your ajax call without specifying the 'dataType' attribute, then control should come back to your success callback if your ajax call completes successfully.

FYI: jQuery will try to find the response data type based on the MIME type of that response.

Example:

$( function() {
  $.ajax({
    url :"http://example.com:8999/123",
    type: 'GET',
    success: function (data) {
     console.log(data);  // Prints the response on console
     alert('success'); 
    }
  })
});

If you want to make this call only with JSONP then it would be better to share the reason with us, so that we can suggest a better solution if possible.

Siva
  • 1,123
  • 2
  • 14
  • 25