开发者

Help parsing json

I have some really simple json I need to parse and then run conditional statements on. The json looks like:

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

And I am trying to parse it with:

开发者_如何学Go
$('.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);
        }

        });
    });

However I just get an error that thejson is not defined.

Thanks in advance for any help.


This looks like JSONP. It is a retrieval technique to allow JavaScript code to call for and receive JSON data from an external domain.

thejson is the callback function, which you will need to define in your JavaScript code (it is the absence of this function that is causing the error). Then, that JSON/JavaScript you are getting back needs to be inserted in script tag into the DOM. At that point the thejson function will be called with the JSON object as a parameter.

jQuery can make JSONP easy to handle.

You probably want something like this:

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',
        });
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜