Can't figure out jQuery ajax call parameters
I am learning jQuery and trying the following but the parameters are so foreign to me with all the embedded quotes I think that is my problem. Can someone explain the parameters and where quotes go and possibly rewrite my parameters line? (This is a live site to see the required parms).
function AirportInfo() {
var divToBeWorkedOn = '#detail';
var webMethod = "'http://ws.geonames.org/citiesJSON'";
var parameters = "{'north':'44.1','south':'9.9','east':'22.4'开发者_Python百科,'west':'55.2','lang':'de'}";
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
$(divToBeWorkedOn).html(msg.d);
},
error: function(xhr) {
alert(xhr);
alert(xhr.statusText);
alert(xhr.responseText);
$(divToBeWorkedOn).html("Unavailable");
}
});
}
It looks like you are going to have a problem with the same origin policy.
In a nutshell, the policy prevents submitting an AJAX request across pages on different domains.
You should probably be using JSONP for Geonames, as described in the following Stack Overflow post:
- JSONP callback fails, need help with javascript/jquery
Apart from that, you wouldn't have needed the single quotes here:
var webMethod = "http://ws.geonames.org/citiesJSON";
Try this way
var divToBeWorkedOn = '#detail';
var webMethod = "'http://ws.geonames.org/citiesJSON'";
var parameters = {'north':'44.1','south':'9.9','east':'22.4','west':'55.2','lang':'de'};
$.ajax({
'type': "POST",
'url': webMethod,
'data': parameters,
'contentType': "application/json; charset=utf-8",
'dataType': "json",
'success': function(msg) {
alert(msg.d);
$(divToBeWorkedOn).html(msg.d);
},
'error': function(xhr) {
alert(xhr);
alert(xhr.statusText);
alert(xhr.responseText);
$(divToBeWorkedOn).html("Unavailable");
}
});
I always use the way below. See if that works for you. I changed your code to be more like I'd do:
var divToBeWorkedOn = '#detail';
var webMethod = "http://ws.geonames.org/citiesJSON";
var parameters = { north:'44.1',south:'9.9', east:'22.4', west:'55.2',lang:'de' };
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
$(divToBeWorkedOn).html(msg.d);
},
error: function(xhr) {
alert(xhr);
alert(xhr.statusText);
alert(xhr.responseText);
$(divToBeWorkedOn).html("Unavailable");
}
});
i always write parameters like this:
data: "north=33.4&south=3"..... ,
精彩评论