使用 jQuery 的 CORS 请求 - $.ajax()
CORS request using jQuery - $.ajax()
我正在开发一个数据来自不同域的Web应用程序。我的意思是在我的应用程序中,几乎 90% 的请求是跨域请求。
在 IIS 上部署此应用程序时,我无法获取数据。
服务器部署在 http://some.ip.add/crmservice客户端部署在 http://diffent.ip.add/saascrm
我正在使用 jQuery 2.0 使用 $.ajax() 以异步方式获取数据;
注意:数据以 xml 格式提供
还向web.config文件添加了一些东西。
<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);
}
}
});
我的服务器代码是:
全球
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", "");
}
我也找到了一些解决方案,我发现IE 8和9不支持CORSs。IE 8 * 9 不会创建 XMLHttpRequest 对象的实例。它创建XDomainRequest,因此需要检查用户代理。我在这里找到了替代解决方案
现在我的问题是我在任何地方都使用 $.ajax() 方法,几乎 90% 的调用是跨域调用。我不想在我的框架中进行此重大更改。
使用 $.ajax() 有什么解决方案吗?
请帮助我,我从一周以来就被严重困住了。
提前谢谢。
感谢您为我提供的所有合作和帮助。我找到了解决方案。
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());
}
}
});
相关文章:
- ajax请求的顺序总是不同的
- 从ajax请求中获取javascript对象
- Ajax请求文档就绪会导致jquery加载缓慢
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- JavaScript代码未正确检查ajax请求
- "日期“;AJAX请求返回的类型值未定义
- 在等待ajax请求时显示微调器并禁用页面
- 跨域ajax请求
- Ajax请求返回空的数据字符串,但首先得到了正确的数据
- Javascript-如何让脚本与Ajax请求的数据一起运行
- ajax请求成功,但可以'我看不到我的数据
- 如何将给定的curl命令复制为jquery ajax请求
- 为什么我能够从javascript控制台发送跨域ajax请求
- 一台特定计算机的Ajax请求数据未定义/失败
- 获取ajax请求的请求头
- 原型Ajax请求参数为嵌套json
- 可以't从AJAX请求中筛选数据
- jQuery Mobile Collapse上的AJAX请求
- 暂停函数执行流程,直到ajax请求完成