简体   繁体   中英

how to prevent JSONP parse from the AJAX call?

I have some code to call couple of other sites (cross domain) with jquery 1.9, which forced me to use jsonp instead of json in an ajax call:

.ajax({ 
type : "POST",
url : "http://web.company.com/login/?login_only=1&callback=photos",
data : "user="+ username,
crossDomain : true,  
dataType : "jsonp",

However, it always returned a parse error:

SyntaxError: missing ; before statement {"status":1,"redirect":"/cpsess9236070602/web/x3/index.html?login

I know it might because the response is not a jsonp format strictly. How can I capture the redirect variable here without a jsonp error?

If the response is not JSONP then there's nothing you can do unless you control the server side.

No parsing actually happens to a JSONP response. A JSONP response is simply a function call with a JavaScript object or array as the argument. jQuery will include the response as a <script> tag, executing the JavaScript and so if there is no function call (as such with a regular JSON response) then you can't access the content.

A possible solution could be to proxy the request through a server side script on the same domain as your JavaScript, using a server to server call for the third party, which won't be subject to cross-domain restrictions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM