chad larson
chad larson

Reputation: 21

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','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");
       }
});
}

Upvotes: 2

Views: 932

Answers (4)

André Miranda
André Miranda

Reputation: 6588

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");
    }
});

Upvotes: 1

Boris Barroso
Boris Barroso

Reputation: 1812

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");
    }
});

Upvotes: 1

Daniel Vassallo
Daniel Vassallo

Reputation: 344291

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:


Apart from that, you wouldn't have needed the single quotes here:

var webMethod = "http://ws.geonames.org/citiesJSON";

Upvotes: 2

loviji
loviji

Reputation: 13080

i always write parameters like this:

data: "north=33.4&south=3"..... ,

Upvotes: 0

Related Questions