parsererror after jQuery.ajax request with jsonp content type

JSONP requires that the response be wrapped in some kind of callback function, because it works by injecting a script tag into the document as a mechanism to load data from another domain.

Essentially, what happens is a script tag gets dynamically inserted into the document like so:

<script src="http://the.other.server.com/foo?callback=someFn"></script>

callback is dependent on the resource you're calling, it's common for the parameter to be callback though.

someFn is then used to process the returned data from the server, so the server should respond with:

someFn({theData: 'here'});

The someFn is passed as part of the request, so the server needs to read it and wrap the data appropriately.

This is all assuming you're grabbing the content from another domain. If so, you're limited by the same origin policy: http://en.wikipedia.org/wiki/Same_origin_policy


After upgrading to Jquery 1.5 and attempting to make a call across domains I had the same problem. Eventually I found the $.getJSON worked. Specifically,

$.getJSON(url,
    function(data){
        yourFunction(data);
       return false;
    });

The URL I used was like this:

var url = WEB_SERVER_URL;
url = url + "&a=" + lat;
url = url + "&b=" + lng; ....
url = url + "&jsoncallback=?";

In the server, which is running on another server and I have control of this code was added:

PrintWriter writer = response.getWriter();
String jsonString = json.toString(JSON_SPACING);
String callback = request.getParameter("jsoncallback");
// if callback in URL and is not just the "?" (e.g. from localhost)
if (callback != null && callback.length() > 1)
{
    writer.write(callback + "(" + jsonString + ");");
}
else
{
    writer.write(jsonString);
}

(The json object is an instance of JSONObject, the code can be found here http://www.json.org/java/)


when you are using jsonp as datatype (making cross domain request) Jquery generate random function and append is to requested url as a querystring named callback (callback=?), you need to append response json data as a parameter of this function as given below -

url : http://www.dotnetbull.com/cross-domain-call.ashx?ref=jquery-jsonp-request
url call by ajax :
http://www.dotnetbull.com/cross-domain-call.ashx?ref=jquery-jsonp-request&callback=jQuery1510993527567155793_137593181353

Response data should look like this :

 string callback = context.Request.QueryString["callback"];

 if (!string.IsNullOrEmpty(callback))
   context.Response.Write(string.Format("{0}({1});", callback, jc.Serialize(outputData)));
else
   context.Response.Write(jc.Serialize(outputData));

Read more about : parsererror after jquery.ajax request with jsonp content type

enter image description here