tags:

views:

15

answers:

1

I am getting an Invalid Error Label in my console error message.

My result.json file is in format like

{
"name": "Zara Ali",
"age" : "67",
"sex": "female"
}

and my code is like below from where i want to fetch above result.joson file

$(document).ready(function() {  

    //if submit button is clicked  
   $('#recaptcha_reload').click(function () { 

$.ajax({

dataType: "jsonp",

url: 'http://www.remoteserver.com/advertise_api/result.json?callback=?&rpp=50&q=mozilla',

jsonp: "$callback",

success: function(data){

alert("#");

},

error : function(XMLHttpRequest, textStatus, errorThrown) {

          alert("$$");   

        }

  });

});   

});   

What about this error ? Thanks

A: 

When requesting using JSONP, you have to return the data in JSONP format:

$callback({
"name": "Zara Ali",
"age" : "67",
"sex": "female"
});

The data will be executed when it arrives (as that is how JSONP works). If you don't put the object in a function call, it will be executed as if it was code, which is the reason for the error message. The brackets are interpreted as a scope block, and "name": is interpreted as a label, which is invalid because a label can't have quotation marks in the identifier.

Guffa
Can u explain a bit more plz ? where i should to change ? In Json file structure ?
Ajay_kumar
Yes, I showed you how to put the function call around the data to turn your JSON format into JSONP. The function name `$callback` is what you have specified in your call to `$.ajax`, so that is the callback function that jQuery will set up.
Guffa
when i changed my result.json file with the above code i am getting an error $callback is not defined.
Ajay_kumar
thanks now this is connecting..... I wrote $callback as function name and same function written to json file and this is connecting now. is this right ?
Ajay_kumar
@Ajay_kumar: I see, the `jsonp` property changes the parameter key in the querystring, not the function name. You should have `jsonpCallback: "$callback"` in your ajax call instead of the `jsonp` property.
Guffa