简体   繁体   English

如何将参数传递给 jQuery $.getJSON 回调方法?

[英]How can I pass parameters to a jQuery $.getJSON callback method?

I'm trying to use jQuery to call some custom API via Ajax/$.getJSON .我正在尝试使用 jQuery 通过Ajax/$.getJSON调用一些自定义 API。

I'm trying to pass a custom value into the Ajax callback method, but that value is not getting passed through and is actually getting overwritten.我正在尝试将自定义值传递到 Ajax 回调方法中,但该值并未传递,实际上已被覆盖。 This is my code:这是我的代码:

var locationType = 3;
var url = 'blah blah blah' + '&locationType=' + locationType;

$("#loading_status").show();

$.getJSON(url, null, function(results, locationType) {
    searchResults(results, locationType)
});

The value of locationType BEFORE I call the URL using AJAX is 3 .在我使用 AJAX 调用 URL 之前, locationType的值为3 But after the call returns the data successfully, the value for locationType is now success .但是在调用成功返回数据后, locationType的值现在是success This is because the method signature of the callback is:这是因为回调方法签名是:

callback(data, textStatus)A callback function that is executed if the request succeeds. callback(data, textStatus) 请求成功时执行的回调函数。

How can I pass 1 or more parameters to a callback method?如何将 1 个或多个参数传递给回调方法?

Warp in a function, eg扭曲函数,例如

function getResults(locationType) {
    $.getJSON(url, null, function(results) {
        searchResults(results, locationType)
    });
}

But in you specific situation you don't even have to pass it, you can access the value directly in the callback.但是在您的特定情况下,您甚至不必传递它,您可以直接在回调中访问该值。

You don't need to pass it in, just reference the variable you already have, like this:您不需要传入它,只需引用您已有的变量,如下所示:

var locationType = 3;
var url = 'blah blah blah' + '&locationType=' + locationType;
$("#loading_status").show();
$.getJSON(url, null, function(results) {
    searchResults(results, locationType)
});

Also there's no need to pass null if you don't have a data object, it's an optional parameter and jQuery checks if the second param is a function or not, so you can just do this:此外,如果您没有数据对象,则无需传递null ,它是一个可选参数,jQuery 会检查第二个参数是否为函数,因此您可以这样做:

$.getJSON(url, function(results) {
    searchResults(results, locationType)
});

You could use the .ajax method:您可以使用.ajax方法:

var locationType = 3;
var url = 'blah blah blah' + '&locationType=' + locationType;
$.ajax({
    url: url,
    context: { lt: locationType },
    success: function(results) {
        searchResults(results, this.lt);    
    }
});

If you want to use locationType (whose value is 3 ) in the callback, simply use如果您想在回调中使用locationType (其值为3 ),只需使用

function(results) { .....

thanks to closures , locationType will be automatically available in the callback.多亏了closureslocationType将在回调中自动可用。

Could try:可以试试:

function getResults(locationType) {
    $.getJSON(url, {p1:'xxx', p2: 'yyy'}, function(results) {
        searchResults(results, locationType)
    });
}
$.getJSON("url",
        "//here you can define your id, secure key to check data in your console, if you don't have condition for id and secure key just remove the secure key part."
        {secure_key:"noaccess"}, function(data){
            console.log(data);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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