Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: invalid label using jQuery.ajax()

Tags:

jquery

ajax

jsonp

I'm trying to make an Ajax call to my server using the following jQuery call:

$.ajax({
    type: "GET",
    dataType: "jsonp",
    url: "http://iceworld.sls-atl.com/api/&cmd=time",
    success: function (data) {
        console.log("success");
        console.log(data);
    },
    error: function (error) {
        console.log("error");
        console.log(error);
    },
});

I get the data that I expect from the browser, but Firebug keeps saying "SyntaxError: invalid label" as shown below:

invalid label

So, what puzzles me is the reason why the error callback is called instead of success. I'm wondering what I did wrong here.

like image 688
sivabudh Avatar asked Jan 02 '26 08:01

sivabudh


2 Answers

JSONP data must be returned in the format: callback( jsonObject ). That is why you are getting an invalid label error. It is expecting a function, not a JSON object. You need to modify your server code to wrap the return value with the name of the callback function. The name is automatically added to the request by jQuery when you request JSONP. If you watch the request, you should see something like this:

http://iceworld.sls-atl.com/api/&cmd=time?callback=jQuery191035087670385837555_1365126604422&_=1365126604423

Your script needs to take the callback parameter and use that to wrap the data, so for this example it would look like this:

jQuery191035087670385837555_1365126604422({"status":1,"data":"1365126534"})

If you are accessing the server from the same origin, you could simply use JSON instead.

like image 163
Jeff B Avatar answered Jan 03 '26 22:01

Jeff B


It's hard to clearly answer without more details and without the response sent by the server. But looking at this, it looks like the server is returning valid JSON; not a JSONP wrapper function. Try to change the dataType to JSON (no promise though, would need more info to be sure)

like image 35
Simon Boudrias Avatar answered Jan 03 '26 22:01

Simon Boudrias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!