CORS - 跨域Ajax没有JSONP允许原产地在服务器 [英] CORS - Cross-Domain AJAX Without JSONP By Allowing Origin On Server

查看:130
本文介绍了CORS - 跨域Ajax没有JSONP允许原产地在服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有同样的服务器上的两个独立的应用程序,与EmberJS一个都试图做跨域调用我的后端API。

设置我的后台API,允许从特定的原产地跨域请求。有没有办法然而,为了避免使用JSONP这样的设置? $。阿贾克斯阻止跨域请求他们曾经被发送之前。如果不是,有什么意义CORS的,哪些服务器端我实施了接受我的JS前端源请求?

修改

AJAX请求:

  $。阿贾克斯({
网址:api.lvh.me:3000/accounts/login
数据:有可信度,
键入:POST,
xhrFields:{
  withCredentials:真
},
成功:函数(响应){
  警报(成功了!);
  的console.log(响应);
  警报(响应);
},
失败:功能(消息){
  警报(失败);
  执行console.log(消息);
  警报(消息);
}
});
 

解决方案

有没有必要使用JSONP如果启用CORS。

 访问控制 - 允许 - 产地:http://www.example.com
 

如果这个头在响应设置,则正常XmlHtt prequest将能够访问响应,就好像它是像相同的域。检查是否这个头设置正确。

我希望这个链接会帮助你,如果你正在使用jQuery <一href="http://stackoverflow.com/questions/5584923/a-cors-post-request-works-from-plain-javascript-but-why-not-with-jquery">A CORS POST请求的工作从简单的JavaScript,但为什么不使用jQuery?

更新: 示例

  VAR XMLHTTP =新XMLHtt prequest();
变种url="https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control";
xmlhttp.open(GET,网址,虚假);
xmlhttp.setRequestHeader(内容类型,应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8);
xmlhttp.send();
 

尝试在任何领域,你会得到响应。

更新的解决方案:

请求URL不包含http://导致了该问题,prepending的http://解决了这个问题。

I have two separate apps on the same server, with the EmberJS one trying to do cross-domain calls to my backend API.

I set up my backend API to allow cross-domain requests from that specific origin. Is there a way however, to avoid using JSONP with such a set up? $.ajax is blocking cross-domain requests before they ever get sent. If not, what is the point of CORS, which server-side I had implemented to accept requests from my JS front-end source?

EDIT

AJAX request:

$.ajax({
url: "api.lvh.me:3000/accounts/login",
data: cred,
type: "POST",
xhrFields: {
  withCredentials: true
},
success: function(response){
  alert('succeeded!');
  console.log(response);
  alert(response);
},
failure: function(message){
  alert("failed");
  console.log(message);
  alert(message);
}
});

解决方案

There is no need to use JSONP if you enable CORS.

Access-Control-Allow-Origin: http://www.example.com

if this header is set in the response, then normal XmlHttpRequest will be able to access the response as if it is like same domain. Check whether this header is set correctly.

I hope that this link will help you if you are using jquery A CORS POST request works from plain javascript, but why not with jQuery?

Update: Example

var xmlhttp= new XMLHttpRequest();
var url="https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control";
xmlhttp.open("GET",url,false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.send();

Try this in any domain, you will get response.

Update solution:

Request url without "http://" caused the problem, prepending "http://" solved the issue

这篇关于CORS - 跨域Ajax没有JSONP允许原产地在服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆