使用jQuery的CORS请求-$ .ajax() [英] CORS request using jQuery - $.ajax()
问题描述
我正在开发一个Web应用程序,其中数据来自不同的域.我的意思是,在我的应用程序中,几乎90%的请求都是跨域请求.
I am developing a web application where data is coming from different domain. I mean in my application almost 90% request are cross domain request.
在IIS上部署此应用程序时,我无法获取数据.
I am unable to get the data while deploy this application on IIS.
服务器部署在 http://some.ip.add/crmservice 上 客户端部署在 http://diffent.ip.add/saascrm
Server is deployed on http://some.ip.add/crmservice client is deployed on http://diffent.ip.add/saascrm
我正在使用jQuery 2.0以$ .ajax();异步方式获取数据.
I am using jQuery 2.0 to get the data in async manner using $.ajax();
注意:数据以xml格式显示
还向web.config文件中添加了一些内容.
Added some stuff to web.config file also.
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
这是我的摘录.
$.support.cors = true;
$.ajax({
type: "GET",
url: 'http://some.ip.add/crmservice/crmservice.asmx/HandShake',
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain: true,
beforeSend: function (request) {
// debugger;
request.setRequestHeader("Access-Control-Allow-Origin", '*');
},
error: function (xhr, status, error) {
try {
// debugger;
// debugger;
//Here i am getting error : Access denied in IE 9.0 and and just "error" in firefox.
var msg = JSON.parse(xhr.responseText);
alert(msg.Message);
}
catch (e) {
// debugger;
alert(xhr.statusText);
}
return true;
},
success: function (data) {
debugger;
xmlDoc1 = $.parseXML(data.d);
$xml1 = $(xmlDoc1);
if ($xml1.find('Result').text() == '0') {
$(this).MessageBox('success', $xml1.find('Message').text());
$("#uxDBName").prop("disabled", false);
$("#uxSUPassword").prop("disabled", false);
$("#uxServiceURL").prop("disabled", true);
GetListOfB1Databases(url);
}
}
});
我的服务器代码是:
Global.asax
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", "");
}
I found some solutions on this also, I found that CORS is not supported by IE 8 & 9. IE 8 * 9 does not create the instance of XMLHttpRequest object. It create XDomainRequest, so need to check for the user agent. I found an alternate solution here
现在我的问题是,我几乎到处都有将$ .ajax()方法用于跨域调用.我不想在我的框架中进行重大更改.
Now my problem is I have use $.ajax() method everywhere almost 90% call is cross domain call. I don't want to make this major change in my framework.
使用$ .ajax()对此有解决方案吗?
Is there any solution to this using $.ajax()?
请帮助我,自一个星期以来,我一直处于严重困境.
Please help me, I am badly stuck since a week.
谢谢.
推荐答案
感谢您的所有合作和帮助. 我找到了解决方法.
Thanks for all of your cooperation and help that you provide me. I found solution.
var url = $("#uxServiceURL").val();
$.ajax({
crossOrigin: true,
url: url + '/HandShake',
error: function (xhr, status, error) {
try {
alert('Error');
}
catch (e) {
alert(xhr.statusText);
}
return true;
},
success: function (data) {
var d1 = data.replace(/\</g, '<').replace(/\>/g, '>')
xmlDoc1 = $.parseXML(d1);
$xml1 = $(xmlDoc1);
if ($xml1.find('Result').text() == '0') {
$(this).MessageBox('success', $xml1.find('Message').text());
}
}
});
这篇关于使用jQuery的CORS请求-$ .ajax()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!