AJAX jQuery的PHP返回值 [英] AJAX jQuery PHP Return Value

查看:180
本文介绍了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);
          }
 });

这篇关于AJAX jQuery的PHP返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆