问题
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:

So, what puzzles me is the reason why the error callback is called instead of success. I'm wondering what I did wrong here.
回答1:
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.
回答2:
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)
回答3:
Your json return value should be in the format function_name(json_data).
like this:
return({"status":a,"data":"123456789"})
create a function as 'return' or same name give with json return value. and the return function will be called automatically when there is a response.
$.ajax(
{
type: "GET",
dataType: "jsonp",
url: "http://iceworld.sls-atl.com/api/&cmd=time",
error: function(error)
{
console.log("error");
console.log(error);
},
} );
function return()
{
console.log(data);
}
hop it will work fine
来源:https://stackoverflow.com/questions/15824421/syntaxerror-invalid-label-using-jquery-ajax