简体   繁体   中英

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

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

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

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM