Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems with jQuery, ajax and jsonp

I am using jsonp and ajax to query a web-service written in java on another server. I am using the following jquery command:

$.ajax({
    type: "GET",
    url: wsUrl,
    data: {},
    dataType: "jsonp",
    complete: sites_return,
    crossDomain: true,
    jsonpCallback: "sites_return"
});


function jsonp_callback(data) {
    console.log(data);
}

function sites_return(data) {
    console.log(data);
}

So my problem is that after the query finishes a function called jsonp_callback is called. Where I can clearly see the json formatted string:

{"listEntries":["ELEM1", "ELEM2", "ELEM3", etc...]}

But after the function sites_return is called when the complete event fires, I get the the following:

Object { readyState=4, status=200, statusText="parsererror"}

Also for reference the jsonp_callback function is called before the sites_return function. Also if i take the jsonp_callback function out of the code, I get a complaint it firebug that the function is not implemented.

My question three fold: 1) What am i doing wrong on the jquery side? 2) Why does the json get parsed correctly in jsonp_callback but not sites_return? 3) What can i do to fix these issues?

EDIT

Some new development. Per the comments here is some additional information.

The following is what comes out of the http response

jsonp_callback({"listEntries":["ELEM1", "ELEM2", "ELEM3"]})

I assume this is the reason jsonp_callback is being called. I guess my question now becomes, is there any way to control this (assuming i don't have access to the back end web-service).

like image 314
sumone4life Avatar asked Oct 10 '22 18:10

sumone4life


2 Answers

Hope this helps~

var url = "http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false";
var address = "1600+Amphitheatre+Parkway";
var apiKey = "+Mountain+View,+CA";

$.getJSON("http://maps.google.com/maps/geo?q="+ address+"&key="+apiKey+"&sensor=false&output=json&callback=?",
  function(data, textStatus){
     console.log(data);
  });
like image 161
Jinbom Heo Avatar answered Oct 13 '22 09:10

Jinbom Heo


I believe that the first argument to the sites_return function would be the jqXHR Object. Instead of complete try using success.

But still this may not work as it seems that there is a parsing error (mentioned in the return value of sites_return function called from oncomplete). Therefore, you would first need to check your json string.

To Validate JSON, you can use http://jsonlint.com/

like image 26
sv_in Avatar answered Oct 13 '22 10:10

sv_in