使用jQuery的.ajax时,您使用什么词来引用检索到的数据?

时间:2016-06-19 16:58:07

标签: jquery ajax

例如,假设检索到的数据是整数2,我们希望使用以下代码将此值赋给变量'num':

$.ajax({
url: "index.php",
dataType: "JSON",
success: function(){
    var num = ???;
    }
})

你会用什么代替'???' ?

2 个答案:

答案 0 :(得分:2)

响应作为参数传递给回调。只需定义一个参数:

success: function(response){
    // "response" contains the response from the server
}

当然,您可以随意调用变量。

答案 1 :(得分:0)

再给出一个更详细的例子:

$.ajax({
    url: "index.php",
    dataType: "JSON", //dataType only modifies what is coming BACK from PHP side
    success: function(recd){
        //if (recd.length > 0) alert(recd);
        var num = recd.number; //or whatever that variable is called in the JSON
    }
});