CORS - 跨域AJAX无JSONP通过允许服务器上的起源 [英] CORS - Cross-Domain AJAX Without JSONP By Allowing Origin On Server

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

问题描述

我在同一台服务器上有两个单独的应用程序,EmberJS尝试对我的后端API进行跨域调用。



我设置了我的后端API允许来自该特定来源的跨域请求。有没有办法,以避免使用这样一个设置的JSONP? $。ajax 在发送之前阻止跨域请求。如果没有,CORS哪个服务器端实现了接受JS前端源的请求?



编辑



AJAX请求:

  $。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);
}
});


解决方案

如果启用CORS,则无需使用JSONP 。

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

如果这个头在响应中设置,那么正常的XmlHttpRequest将能够访问响应,就好像它是同一个域。检查这个标题是否正确设置。



我希望这个链接可以帮助你,如果你使用jquery 一个CORS POST请求可以使用简单的javascript,但是为什么不使用jQuery?



更新:
示例

  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();

在任何域中尝试此操作,您将收到回复。



更新解决方案:



请求没有http://的url导致问题,前缀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天全站免登陆