AJAX jQuery的PHP返回值
我是新来的AJAX和那种很困惑什么是PHP传回的jQuery。 所以,你有一个AJAX功能是这样的:
I am new to AJAX and am kind of confused by what PHP passes back to the jQuery. So you have an AJAX function like this:
$.ajax({ url: '/my/site',
data: {action: 'test'},
type: 'post',
success: function(output) {
alert(output);
}
});
(我把这个从阿贾克斯另外一个计算器页。)
(I took this from ajax another StackOverflow page.)
但在各种其他资源,他们将有成功的部分是这样的:
But on various other resources they will have the success section look like this:
success: function(data) {functionfoocommandshere}
我只是困惑,是什么决定了这个变量的命名?如果PHP最终都数组:
I am just confused as to what dictates the naming of this variable? If the PHP ultimately echoes an array:
echo $myVar;
我怎样才能得到这个从AJAX?
How can I get this from the AJAX?
这是Ajax的请求获取整个网站。所以,你不会得到任何变量的数据,但在数据参数整个网站。你做了所有回波将在这个参数。如果你想获取一个数组,你应该把它转换成JSON之前。
An Ajax-Requests fetches a whole site. So you'll not get any data in variables, but the whole site in the data-parameter. All echos you made together will be in this parameter. If you want to retrieve an array, you should transform it to json before.
echo json_encode($myArray);
然后就可以用这种方法通过Ajax收到
Then you can receive it via Ajax in this way
$.ajax({ url: '/my/site',
data: {action: 'test'},
dataType: 'json',
type: 'post',
success: function(output) {
alert(output);
}
});