Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsererror after jQuery.ajax request with jsonp content type

I am using jQuery Version 1.5.1 to do the following ajax call:

$.ajax({     dataType: 'jsonp',     data: { api_key : apiKey },     url: "http://de.dawanda.com/api/v1/" + resource + ".json",     success: function(data) { console.log(data); },     error: function(jqXHR, textStatus, errorThrown) { console.log(errorThrown); console.log(textStatus); } }); 

The server responds with a valid json object:

{   "response": {     "type":"category",     "entries":1,     "params":{       "format":"json",       "api_key":"c9f11509529b219766a3d301d9c988ae9f6f67fb",       "id":"406",       "callback":"jQuery15109935275333671539_1300495251986",       "_":"1300495252693"     },     "pages":1,     "result":{       "category":{         "product_count":0,         "id":406,         "restful_path":"/categories/406",         "parent_id":null,         "name":"Oberteile"        }      }    }  } 

But the success callback is never called, instead the error callback produces this output:

jQuery15109935275333671539_1300495251986 was not called parsererror 

Why does this happen?

I am using no additional libraries to jQuery.

EDIT:

If I try to make the ajax call with "json" as dataType instead of "jsonp", the server responds with an empty string.

like image 930
Thomas Avatar asked Mar 19 '11 00:03

Thomas


People also ask

What is JSONP in Ajax?

JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.

What is a JSONP response?

The response is a javascript loaded on to your browser with name of the pre-defined function along with parameter being passed that is tht JSON data being requested. When the script executes, the function is called along with JSON data, allowing the requesting page to receive and process the data.

What is JSONP jQuery?

JSONP (which stands for JSON with Padding) builds on this technique and provides us with a way to access the returned data. It does this by having the server return JSON data wrapped in a function call (the “padding”) which can then be interpreted by the browser.

What is textStatus in Ajax?

The textStatus is the textual status message returned by the server. The errorThrown parameter is the error thrown by jQuery. The callback function passed to the always() function is called whenever the AJAX request finishes, regardless of whether or not the AJAX request succeeds or fails.


2 Answers

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

like image 172
Evan Trimboli Avatar answered Sep 18 '22 15:09

Evan Trimboli


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/)

like image 44
Bryan Avatar answered Sep 18 '22 15:09

Bryan