Jquery AJAX (json)跨域请求和ASP。NET MVC

时间:2022-12-04 17:39:41

Seemed to me to be a beaten theme, but i couldn't find the answer. =( I make jquery ajax requst to localhost:666 from localhost:555 application

在我看来,这是个老生常谈的话题,但我找不到答案。=(我将jquery ajax requst添加到localhost:666来自localhost:555应用程序。

    $.ajax({
            url: "http://localhost:666/request",
            dataType: 'json',
            timeout: 5000,
            success:...

i've got in chrome:

我有在铬:

XMLHttpRequest cannot load http://localhost:666/request. Origin http://localhost:555 is not allowed by Access-Control-Allow-Origin.

XMLHttpRequest不能加载http://localhost:666 /请求。访问控制允许的起源不允许访问http://localhost:555。

What is the solution of the problem?

问题的解决方案是什么?

3 个解决方案

#1


7  

You can initiate cross-domain request in your webpage by creating either XMLHttpRequest object or XDomainRequest object. End user's web-browser will request data from the domain's server by sending an "Origin" header with the value of origin. If server responds with an "Access-Control-Allow-Origin: * | Origin" then we are permitted to access data; otherwise response will be unauthorized request.

您可以通过创建XMLHttpRequest对象或XDomainRequest对象来发起跨域请求。最终用户的web浏览器将通过发送带有起源值的“起源”标头从域的服务器请求数据。如果服务器响应“访问控制允许起源:* |起源”,则允许访问数据;否则,响应将是未经授权的请求。

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    // HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://AllowedDomain.com");
}

An article here: Cross-Origin requests and ASP.NET MVC

这里有一篇文章:跨源请求和ASP。NET MVC

#2


-1  

ajax calls are confined to parent domain only. for this a site on localhost:666 can not open ajax connection to localhost:555 since they belongs to different domain (or origin)

ajax调用仅限于父域。对此,localhost:666上的站点不能打开到localhost:555的ajax连接,因为它们属于不同的域(或源)

you need to try jsonp: http://www.google.com/search?q=jsonp

您需要尝试jsonp: http://www.google.com/search?q=jsonp

#3


-2  

Try using dataType: 'jsonp', or $.getJSON function.

尝试使用数据类型:'jsonp'或$。getJSON函数。

#1


7  

You can initiate cross-domain request in your webpage by creating either XMLHttpRequest object or XDomainRequest object. End user's web-browser will request data from the domain's server by sending an "Origin" header with the value of origin. If server responds with an "Access-Control-Allow-Origin: * | Origin" then we are permitted to access data; otherwise response will be unauthorized request.

您可以通过创建XMLHttpRequest对象或XDomainRequest对象来发起跨域请求。最终用户的web浏览器将通过发送带有起源值的“起源”标头从域的服务器请求数据。如果服务器响应“访问控制允许起源:* |起源”,则允许访问数据;否则,响应将是未经授权的请求。

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    // HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://AllowedDomain.com");
}

An article here: Cross-Origin requests and ASP.NET MVC

这里有一篇文章:跨源请求和ASP。NET MVC

#2


-1  

ajax calls are confined to parent domain only. for this a site on localhost:666 can not open ajax connection to localhost:555 since they belongs to different domain (or origin)

ajax调用仅限于父域。对此,localhost:666上的站点不能打开到localhost:555的ajax连接,因为它们属于不同的域(或源)

you need to try jsonp: http://www.google.com/search?q=jsonp

您需要尝试jsonp: http://www.google.com/search?q=jsonp

#3


-2  

Try using dataType: 'jsonp', or $.getJSON function.

尝试使用数据类型:'jsonp'或$。getJSON函数。