帮助解析json

时间:2011-04-14 21:38:36

标签: jquery ajax json

我有一些非常简单的json我需要解析然后运行条件语句。 json看起来像:

thejson(
{"catalog.exists":"0"},"");

我正在尝试解析它:

$('.clicky').click(function(){
        $.ajax({
        type: 'GET',
        url: 'http://myjsonfile.com',
        data: 'req=exists,json',
        dataType: 'jsonp',
        success: function (results) {
        var x= catalog.exists;
        $("#results").append(x);
        }

        });
    });

但是我只是得到一个错误,即没有定义json。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这看起来像JSONP。它是一种允许JavaScript代码从外部域调用和接收JSON数据的检索技术。

thejson是回调函数,您需要在JavaScript代码中定义(缺少此函数会导致错误)。然后,您要返回的JSON / JavaScript需要在脚本标记中插入到DOM中。此时,将使用JSON对象作为参数调用thejson函数。

jQuery可以制作JSONP easy to handle

你可能想要这样的东西:

function thejson(response) {
              var x= response["catalog.exists"];
              $("#results").append(x);
}

$('.clicky').click(function(){
        $.ajax({
        type: 'GET',
        url: 'http://myjsonfile.com',
        data: 'req=exists,json',
        dataType: 'jsonp',
        });
    });