Reputation: 12836
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:
$('.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.
Upvotes: 0
Views: 125
Reputation: 26628
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',
});
});
Upvotes: 1