简体   繁体   English

如何发送带有JSON响应的跨域Ajax请求?

[英]How can i send cross domain ajax request with JSON response?

Guys, 伙计们

I want to send a cross domain ajax request( http://dict.qq.com/dict?q=language ), but the server only provides JSON( Not JSONP ) response, is there anyway to make the request ? 我想发送跨域ajax请求( http://dict.qq.com/dict?q=language ),但是服务器仅提供JSON( 非JSONP )响应,是否仍然可以发出请求?

Here's what i've did: 这是我所做的:

$.ajax({
    url:"http://dict.qq.com/dict?q=language",
    dataType:"jsonp",
    type:'get',
    processData:false,
    crossDomain:true,
    contentType:"application/json",
    success:result
});

function result(data){console.log(data);}

The response is : 响应为:

Uncaught SyntaxError: Unexpected token : 

Thanks ! 谢谢 !

还需要将服务器配置为允许跨域请求,例如:

  Http.Response.current().accessControl("*", "GET,PUT,POST,DELETE,OPTIONS",false);

Generally the same origin policy prevents you from doing so. 通常, 相同的来源策略会阻止您这样做。 If you have access to the other server you can attempt to change the http headers sent to circumvent this. 如果您有权访问其他服务器,则可以尝试更改发送的http标头以规避此问题。 Here's a tutorial for using CORS (Cross-Origin Resource Sharing) to achieve this. 这是使用CORS(跨源资源共享)实现此目标的教程。

If you have no control over the remote server perhaps you can try using a server-side proxy such as this one 如果您无法控制远程服务器,则可以尝试使用服务器端代理(例如代理)

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

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