jQuery $ .post处理JSON响应 [英] jQuery $.post processing JSON response

查看:77
本文介绍了jQuery $ .post处理JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何从jQuery $ .post()请求中正确读取JSON响应时遇到了麻烦.

I'm having trouble figuring out how to properly read my JSON response from a jQuery $.post() request.

在下面的jQuery代码中,我根据对应的"color_entry_id"(用作键)从DOM中的元素填充字符串的关联数组:

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:

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;
});

然后我发出POST请求,发送我的"image_links"数组:

Then I make the POST request, sending my array of "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);
        });
    }
);

此外,如上所示,我尝试遍历响应数组并输出要成为字符串的每个项目,但是我只获得"[object Object]"作为警报值.我不知道如何使其显示我要显示的字符串!

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!

这是test.php的PHP代码:

Here is the PHP code for test.php:

<?php
    $product_id = $_POST['id'];
    $images = $_POST['images'];

    $response = array();
    $response['id'] = $product_id;
    $response['images'] = $images;

    echo json_encode($response);
?>

这是DOM相关部分的样子:

And here's what the relevant part of the DOM looks like:

<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>

我想知道我是在PHP端执行错误的JSON编码还是在jQuery中错误地遍历响应.任何帮助深表感谢!

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!!

推荐答案

然后..从帖子中获取的数据对象是:{"id":"abc","images":[{"color123":"somelink.com\/123","color223":"somelink.com\/‌​223"}]};

Ok then..the data object you're getting back from the post is: {"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]);
        }
    }
);

这篇关于jQuery $ .post处理JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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