Abdallah
Abdallah

Reputation: 401

How to pass parameters to a JQuery $.getJSON callback method?

 function CallMethod() {
     $.getJSON('/website/RESTfulService.svc/LiveLocation/json?{x=1,y=2}', function(data) {
         getResult(data.lat, data.lon);
     });
 }

Upvotes: 40

Views: 88998

Answers (3)

Rahul Srivastava
Rahul Srivastava

Reputation: 373

Alternatively, first create javascript object for the sake of simplicity and then pass

var myObject = {x: "1", y: "2"};

$.getJSON('/website/RESTfulService.svc/LiveLocation/json', myObject, function(dataVal) {
    //Use Your result
});

Upvotes: 11

Tal Tikotzki
Tal Tikotzki

Reputation: 403

Just like Zheileman said, but note that this way, although you passed the parameters in JSON format, the actual parameters are passed to the webserver as an Encoded HTTP URL which will end up this way:

/website/RESTfulService.svc/LiveLocation/json?x=1&y=2

Upvotes: 4

Zheileman
Zheileman

Reputation: 2559

Pass them as an object just after the URL and before the function:

function CallMethod() {
     $.getJSON('/website/RESTfulService.svc/LiveLocation/json', 
     {
        x: "1",
        y: "2"
     }, 
     function(data) {
         getResult(data.lat, data.lon);
     });
}

Upvotes: 94

Related Questions