Javascript 使用jQuery-$.ajax()的CORS请求
我正在开发一个web应用程序,其中数据来自不同的领域。我的意思是,在我的应用程序中,几乎90%的请求都是跨域请求 在IIS上部署此应用程序时,我无法获取数据 服务器部署在上 客户端部署在 我使用jQuery2.0以异步方式使用$.ajax获取数据 注意:数据是xml格式的 还向web.config文件添加了一些内容Javascript 使用jQuery-$.ajax()的CORS请求,javascript,jquery,asp.net,ajax,cors,Javascript,Jquery,Asp.net,Ajax,Cors,我正在开发一个web应用程序,其中数据来自不同的领域。我的意思是,在我的应用程序中,几乎90%的请求都是跨域请求 在IIS上部署此应用程序时,我无法获取数据 服务器部署在上 客户端部署在 我使用jQuery2.0以异步方式使用$.ajax获取数据 注意:数据是xml格式的 还向web.config文件添加了一些内容 <system.webServer> <httpProtocol> <customHeaders> <a
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
我的服务器代码是:
Global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
EnableCrossDmainAjaxCall();
}
private void EnableCrossDmainAjaxCall()
{
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Headers","Content-Type, Accept");
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
HttpContext.Current.Response.AppendHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
//Web method
[ScriptMethod(ResponseFormat = ResponseFormat.Json), WebMethod(EnableSession = true)]
public string HandShake()
{
return General.Response("0", "Tenant is in reachable. Please specify SAP Business One Company database\r\nand 'manager' Password", "");
}
我还找到了一些解决方案,我发现IE8&9不支持CORS。
IE 8*9不创建XMLHttpRequest对象的实例。它创建XDomainRequest,因此需要检查用户代理。我找到了另一个解决办法
现在我的问题是我到处都在使用$.ajax方法,几乎90%的调用都是跨域调用。我不想在我的框架中做这个重大的改变
使用$.ajax有什么解决方案吗
请帮帮我,我一个星期以来一直困在这里
提前感谢。感谢您为我提供的所有合作和帮助。 我找到了解决办法 var url=$uxServiceURL.val; $.ajax{ 交叉起源:对, url:url+'/握手', 错误:函数xhr、状态、错误{ 试一试{ 警告“错误”; } 抓住e{ alertxhr.statusText; } 返回true; }, 成功:函数数据{ 变量d1=数据。替换/\/g, xmlDoc1=$.parseXMLd1; $xml1=$xmlDoc1; 如果$xml1.find'Result'。text=='0'{ $this.MessageBox'success',$xml1.find'Message'.text; } }
}; 嗯,JSON!==XML您说过要返回XML,但您的Ajax代码表明您需要JSON。我不认为xml是跨域访问的。访问跨域数据需要AFAIK jsonp,数据应为json格式。@Jai,使用CORS并不重要。@epascarello事实上我以前从未遇到过这种情况,也从未使用xml查找过。谢谢,我现在就去找。
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
EnableCrossDmainAjaxCall();
}
private void EnableCrossDmainAjaxCall()
{
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Headers","Content-Type, Accept");
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
HttpContext.Current.Response.AppendHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
//Web method
[ScriptMethod(ResponseFormat = ResponseFormat.Json), WebMethod(EnableSession = true)]
public string HandShake()
{
return General.Response("0", "Tenant is in reachable. Please specify SAP Business One Company database\r\nand 'manager' Password", "");
}