简体   繁体   English

如何防止来自 AJAX 调用的 JSONP 解析?

[英]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:我有一些代码可以使用 jquery 1.9 调用其他几个站点(跨域),这迫使我在 ajax 调用中使用 jsonp 而不是 json:

.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语句之前 {"status":1,"redirect":"/cpsess9236070602/web/x3/index.html?login

I know it might because the response is not a jsonp format strictly.我知道这可能是因为响应严格来说不是 jsonp 格式。 How can I capture the redirect variable here without a jsonp error?如何在没有 jsonp 错误的情况下在此处捕获重定向变量?

If the response is not JSONP then there's nothing you can do unless you control the server side.如果响应不是 JSONP,那么除非您控制服务器端,否则您无能为力。

No parsing actually happens to a JSONP response.实际上没有解析发生在 JSONP 响应上。 A JSONP response is simply a function call with a JavaScript object or array as the argument. JSONP 响应只是一个以 JavaScript 对象或数组作为参数的函数调用。 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. jQuery 会将响应包含为<script>标记,执行 JavaScript,因此如果没有函数调用(例如使用常规 JSON 响应),则您无法访问内容。

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.一个可能的解决方案可能是通过与 JavaScript 相同的域上的服务器端脚本代理请求,使用第三方的服务器到服务器调用,这不会受到跨域限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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