gpt4 book ai didi

asp.net-mvc - Jquery AJAX(json)跨域请求与ASP.NET MVC

转载 作者:行者123 更新时间:2023-12-01 01:04:19 24 4
gpt4 key购买 nike

在我看来,这是一个被打败的主题,但我找不到答案。 =(我发出 jquery ajax 请求来自 localhost:555 应用程序的 localhost:666

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

我已经进入 Chrome 了:

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

问题的解决方案是什么?

最佳答案

您可以通过创建XMLHttpRequest对象或XDomainRequest对象在网页中发起跨域请求。最终用户的网络浏览器将通过发送带有 origin 值的“Origin” header 来向域服务器请求数据。如果服务器响应“Access-Control-Allow-Origin: * | Origin”,那么我们就被允许访问数据;否则响应将是未经授权的请求。

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");
}

这里有一篇文章:Cross-Origin requests and ASP.NET MVC

关于asp.net-mvc - Jquery AJAX(json)跨域请求与ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5968682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com