AJAX jQuery PHP 返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13366204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AJAX jQuery PHP Return Value
提问by eatonphil
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 的新手,对 PHP 传回 jQuery 的内容有些困惑。所以你有一个像这样的 AJAX 函数:
$.ajax({ url: '/my/site',
data: {action: 'test'},
type: 'post',
success: function(output) {
alert(output);
}
});
(I took this from ajax another StackOverflow page.)
(我从 ajax 的另一个 StackOverflow 页面中获取了这个。)
But on various other resources they will have the success section look like this:
但是在其他各种资源上,他们的成功部分将如下所示:
success: function(data) {functionfoocommandshere}
I am just confused as to what dictates the naming of this variable? If the PHP ultimately echoes an array:
我只是对什么决定了这个变量的命名感到困惑?如果 PHP 最终回显一个数组:
echo $myVar;
How can I get this from the AJAX?
我怎样才能从 AJAX 得到这个?
回答by Gnietschow
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.
Ajax 请求获取整个站点。因此,您不会在变量中获得任何数据,而是在数据参数中获得整个站点。您一起制作的所有回声都将包含在此参数中。如果要检索数组,则应先将其转换为 json。
echo json_encode($myArray);
Then you can receive it via Ajax in this way
然后就可以通过ajax这种方式接收了
$.ajax({ url: '/my/site',
data: {action: 'test'},
dataType: 'json',
type: 'post',
success: function(output) {
alert(output);
}
});
回答by woz
In you PHP file, use json_encodeto turn the array into a more convenient format for use in Javascript. So you would have something like:
在您的 PHP 文件中,使用json_encode将数组转换为更方便的格式以在 Javascript 中使用。所以你会有类似的东西:
echo json_encode($myArray);
Then, in your JavaScript, the datavariable of the successmethod will hold the JSON. Use jQuery's parseJSONto convert this to a JavaScript object, which will then be very easy to manipulate. I don't know what you array contains, but you might do something like this:
然后,在您的 JavaScript 中,data该success方法的变量将保存 JSON。使用 jQuery 的parseJSON将其转换为 JavaScript 对象,然后将非常容易操作。我不知道你的数组包含什么,但你可能会做这样的事情:
$.ajax({ url: '/my/site',
data: {action: 'test'},
type: 'post',
success: function(data) {
var obj = jQuery.parseJSON(data);
alert(obj.name[0] === "John");
}
});
Again, the datavariable here will contain anything your PHP outputs, but JSON is a common and convenient way to transfer data back to your JavaScript.
同样,data这里的变量将包含您的 PHP 输出的任何内容,但 JSON 是将数据传输回 JavaScript 的一种常见且方便的方式。
回答by Samuel Cook
<script type="text/javascript">
$.ajax({
url: '/my/site',
data: {action: 'test'},
type: 'post',
success: function(output) {
alert(output);
}
});
</script>
<?php
$action = $_POST['action'];
echo $action;?>
Any output that is printed/echoed will be returned to the success function. This is handy when you want to fill an html container with something that you need to run in real time.
任何打印/回显的输出都将返回到成功函数。当您想用需要实时运行的内容填充 html 容器时,这很方便。
Once you get the hang of this, another option is to use JSON to return variables with values.
一旦你掌握了这个窍门,另一个选择是使用 JSON 来返回带值的变量。
回答by Teena Thomas
The data that's returned from the PHP AJAX function, can be retrieved from the successblock. Here's the manual
从 PHP AJAX 函数返回的数据可以从success块中检索。这是手册
$.ajax({ url: '/my/site',
data: {action: 'test'},
type: 'post',
dataType: 'json',
success: function(output) {
//output is the data returned from PHP AJAX function in JSON format
alert(output);
}
});

