3

抱歉,我知道这个问题很简单,但我不知道如何从返回的字典中获取响应数据:

这是我的 jQuery.get() 方法:

$("#selectDireccion").change(function() {
    $("select option:selected").each(function() {
        if ($(this).index() != 0) {
            valorKeyId = $(this).val()
            $.get("/ajaxRequest?opcion=obtenerSedeKeyId", {
                keyId: valorKeyId
            }, function(data) {
                alert(data)
            });
        }
    });
});​

这是警报打印的内容:

{"name": "First Value", "phone": "434534"}

我应该如何从字典的“名称”键中获取值?

data.name在警报内执行没有任何效果。

谢谢!

4

1 回答 1

5

看来您正在返回一个 JSON 字符串。如果是这种情况,那么您首先需要运行 jQuery 的parseJSON函数:

var d = $.parseJSON(data);
alert(d.name); // Will output the name from the JSON string.

或者,更好的是(根据@calvin L的评论),使用 jQuery getJSON开头:

$.getJSON("/ajaxRequest?opcion=obtenerSedeKeyId",{keyId:valorKeyId}, function(data){
    alert(data.name); // Data already parsed to JSON, outputs the name
});
于 2012-07-03T00:08:34.690 回答