简体   繁体   English

使用JavaScript调用跨域asmx Web服务

[英]Calling cross domain asmx web service using JavaScript

I have written a web service. 我已经写了一个网络服务。 I am calling this web service using JavaScript. 我正在使用JavaScript调用此Web服务。 I am calling this from different domain. 我从不同的领域来称呼它。 For that I have added [System.Web.Script.Services.ScriptService] property in the web service. 为此,我在Web服务中添加了[System.Web.Script.Services.ScriptService]属性。 From JavaScript I am calling the service using XMLHttpRequest . 从JavaScript,我使用XMLHttpRequest调用服务。 I tested it using Firefox and everything was fine when. 我使用Firefox进行了测试,并且一切正常。 But it was not working in IE. 但是它在IE中不起作用。

After some searching I found that this is an issue related to Cross domain calling . 经过一些搜索,我发现这是与跨域调用有关的问题。 I have gone through some of the questions posted here. 我已经解决了这里发布的一些问题。 And then I did the following changes in my code - 然后,我在代码中做了以下更改-

  1. From javaScript I am now calling the service using XDomainRequest . 从javaScript我现在使用XDomainRequest调用服务。

  2. I have added following lines befor the return statements in the web-service - HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*"); HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Credentials", "true"); return result; 我在Web服务中的return语句之前添加了以下几行HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*"); HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Credentials", "true"); return result; HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*"); HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Credentials", "true"); return result;

It is still working fine in firefox. 在Firefox中它仍然可以正常工作。 but in IE8 (as per my knowledge, XDomainRequest will not work in lower versions of IE) it is showing error ( XDomainRequest.onerror ). 但是在IE8中(据我所知,XDomainRequest在较低版本的IE中将不起作用),它显示错误( XDomainRequest.onerror )。

Am I missing something? 我想念什么吗?

The crux of your problem in IE is that XDomainRequest doesn't support the pre-flighting necessary to make a cross-domain request that includes a Content-Type header. IE中问题的症结在于XDomainRequest不支持发出包含Content-Type标头的跨域请求所必需的预检。 I believe that's fixed in IE10, but even IE9 doesn't fully support CORS . 我相信IE10中已解决此问题,但即使IE9也不完全支持CORS

To reliably make cross-domain requests to ScriptServices in browsers that don't support CORS well, a server-side proxy is (unfortunately) your best bet. 为了在不很好支持CORS的浏览器中可靠地向ScriptServices提出跨域请求,(不幸的)最好的办法是使用服务器端代理

Look into JSONP (json with padding). 查看JSONP(带填充的json)。

Question about JSONP: jsonp with jquery 关于JSONP的问题: 带有jQuery的jsonp

Has some more info about it: http://api.jquery.com/jQuery.ajax/ 有关它的更多信息: http : //api.jquery.com/jQuery.ajax/

your web service runs over HTTP right? 您的Web服务通过HTTP运行吗?

I don't recommend using the native XMLHttpRequest to make ajax request, maybe you should use Jquery to do this, I always do in that way and works in all modern browsers: 我不建议使用本机XMLHttpRequest来发出ajax请求,也许您应该使用Jquery来做到这一点,我总是这样做,并且可以在所有现代浏览器中使用:

ie: 即:

function Activate(EmailId, controle) {
    $.ajax({
        type: "POST",
        url: "/Page/Method",
        data: "&EmailId=" + EmailId,
        success: function (message) {
            $(controle).text(message);
        }
    });
}

EDIT: to make cross-domain requests you can use the James Padolsey plug-In, and do something like this: 编辑:要发出跨域请求,您可以使用James Padolsey插件,并执行以下操作:

$('#container').load('http://google.com');

$.ajax({
    url: 'http://news.bbc.co.uk',
    type: 'GET',
    success: function(res) {
        var headline = $(res.responseText).find('a.tsh').text();
        alert(headline);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM