I have a simple JQuery Stockwatcher app, which makes a JSONP request but the callback function is not getting called. I have the JSON call for this app working, but at somepoint it is going to be cross domain, hence I have to make is JSONP.
What am I doing wrong? When I run this code, the Error function gets called.
function loadData(data) {
alert("Load Data");
$.each(data.stocks,function(i,item){
$("#results").append('Title:'+item.symbol+' == Price:'+item.price+'</p>');
});
}
$(document).ready(function(){
var url='http://localhost:8080/StockWatcherServer/stockwatcher/stockPrices?q=';
var query;
$('button').click(function(){
query=$("#query").val();
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
//$.ajaxSetup({ crossDomain: true, scriptCharset: "utf-8" , contentType: "jsonp; charset=utf-8"});
$.ajax({
url : url+query,
type: "GET",
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "loadData",
success: function(data) {alert("Success");},
error: function(data) { alert("Error"); },
});
});
});
Here is my JSON Output, which I can see in Firebug
{"stocks": [
{
"symbol": "IPOD",
"price": 20.2182603350167,
"change": 0.3128265006354697
}
]}
Thanks Rajesh
That is JSON, not JSONP. If you are doing cross-domain JSON request, it must be JSONP not JSON. Different port is cross-domain.
This is the JSONP that your request should be returning based on your code. Additionally, your loadData method will be overridden by jquery IIRC.
loadData({"stocks": [
{
"symbol": "IPOD",
"price": 20.2182603350167,
"change": 0.3128265006354697
}
]})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.