php jQuery $.post 处理 JSON 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10475652/
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
jQuery $.post processing JSON response
提问by Jon Rubins
I'm having trouble figuring out how to properly read my JSON response from a jQuery $.post() request.
我无法弄清楚如何从 jQuery $.post() 请求中正确读取我的 JSON 响应。
In the below jQuery code, I populate an associative array of strings from elements from the DOM based on the corresponding "color_entry_id" which I use as the key:
在下面的 jQuery 代码中,我根据用作键的相应“color_entry_id”从 DOM 中的元素填充了一个关联字符串数组:
var image_links = {};
$(this).find('input[name="color_id"]').each(function() {
var color_entry_id = $(this).val();
var image_link = $(this).parent().find('input[name="edit_image"].' + color_entry_id).val();
image_links[color_entry_id] = image_link;
});
Then I make the POST request, sending my array of "image_links":
然后我发出 POST 请求,发送我的“image_links”数组:
$.post(
"test.php",
{ id: product_id, "images[]": jQuery.makeArray(image_links) },
function(data) {
var response = jQuery.parseJSON(data);
$.each(response.images, function(index, item) {
alert(item);
});
}
);
Also, as shown above, I try to loop through the response array and output each item, which I want to be a string, but I only get "[object Object]" as the alerted value. I don't know how to make it display the strings I'm trying to display!
此外,如上所示,我尝试遍历响应数组并输出每个项目,我希望将其作为字符串,但我只得到“[object Object]”作为警报值。我不知道如何让它显示我想要显示的字符串!
Here is the PHP code for test.php:
这是 test.php 的 PHP 代码:
<?php
$product_id = $_POST['id'];
$images = $_POST['images'];
$response = array();
$response['id'] = $product_id;
$response['images'] = $images;
echo json_encode($response);
?>
And here's what the relevant part of the DOM looks like:
这是 DOM 的相关部分的样子:
<input type='hidden' value='{{ color_entry_id }}' name='color_id' />
<p><img src='{{ color_img_link }}' /></p>
<p>Image Link: <input class='{{ color_entry_id }}' name='edit_image' type='text' size='150' value='{{ color_img_link }}' /></p>
<div class='colors {{ color_entry_id }}'>
<div class='img_color'>
<a href='javascript:void' style='background:...' class='selected'></a>
<p>...</p>
</div>
</div>
I'm wondering whether perhaps I'm doing the JSON encoding incorrectly on the PHP side or if I'm just looping through the response incorrectly in the jQuery. Any help is much appreciated!!
我想知道我是否在 PHP 端错误地执行了 JSON 编码,或者我是否只是在 jQuery 中错误地循环响应。任何帮助深表感谢!!
回答by ltiong_sh
Ok then..the data object you're getting back from the post is: {"id":"abc","images":[{"color123":"somelink.com\/123","color223":"somelink.com\/??223"}]};
好的,那么您从帖子中返回的数据对象是: {"id":"abc","images":[{"color123":"somelink.com\/123","color223":"somelink.com\/??223"}]};
If you change your alert, you'll find the values you're looking for:
如果您更改警报,您将找到您正在寻找的值:
$.post(
"test.php",
{ id: product_id, "images[]": jQuery.makeArray(image_links) },
function(data) {
var response = jQuery.parseJSON(data);
var images = response.images[0];
for (var i in images){
alert(images[i]);
}
}
);
回答by Bryan
$.post expects xml by default, you need to specify the response format
$.post 默认期望xml,需要指定响应格式
$.post(
"test.php",
{ id: product_id, images : jQuery.makeArray(image_links) },
function(response) {
// Response is automatically a json object
for(var i = 0; i < response.images.length; i++) {
alert(response.images[i]);
}
}, 'json' // <-- HERE
);
Also, consider adding a content type header in your php script
另外,考虑在你的 php 脚本中添加一个内容类型标题
<?php
header("Content-type: application/json"); // Adding a content type helps as well
$product_id = $_POST['id'];
$images = $_POST['images'];
$response = array();
$response['id'] = $product_id;
$response['images'] = $images;
echo json_encode($response);
?>
回答by Bruno Ribeiro
A pretty basic example would be something like :
一个非常基本的例子是这样的:
$.post('test.php', {"id": 42}, function (json) {
console.log(json);
}, 'json');
PHP example
PHP 示例
$number = rand(1,$_POST["id"]);
return print json_encode($number);

