在 HTML 页面上的回调函数中访问来自 .NET Webservice 的响应

发布于 2024-08-19 19:17:43 字数 5086 浏览 5 评论 0原文

我正在尝试从 HTML 页面调用 .net Web 服务。该 HTML 页面将托管在不同的服务器上。我为此使用以下 html 代码。 Web 服务代码位于 HTML 代码下方。这段代码在 IE 中运行良好,并且在使用 venkman 调试时在 Mozilla 中运行良好。但在 Firefox 中无法正常执行。我在 xmlDoc 变量、http.responseXML、http.responseText 或 http.status 中没有得到任何内容。

我还在错误控制台中收到此错误“错误:xmlDoc 未定义行:104”

我猜问题是匿名回调函数无法访问外部的任何内容。

enter code here



<script language="JavaScript">      

    var http =  null;<br>
    var isFirefox = false;<br>
    var StrInput;<br>
    var xmlDoc;<br>
   alert('Hi');<br>
function getXMLHTTP()<br>
{<br>
    var httpReq = null;<br>
<br>
    // Internet Explorer<br>
    try<br>
     {
     httpReq = new ActiveXObject("Msxml2.XMLHTTP");<br>
     }<br>
    catch (e)<br>
     {<br>
        try<br>
        {<br>
            httpReq = new ActiveXObject("Microsoft.XMLHTTP");<br>
        } <br>
        catch(oc)<br>
        {<br>
            httpReq = null;<br>
        }<br>
     }<br><br>
    // Firefox, Opera 8.0+, Safari..create object for webservice request<br>
    **if(!httpReq && typeof XMLHttpRequest != "undefined") <br>
        {<br>
            httpReq = new XMLHttpRequest();<br>
            isFirefox = true;<br>
        }**<br>
    return httpReq;<br>
}<br>
    <br>
function callGetLatestPoll()<br>
{<br>
debugger;<br>
    StrInput = document.DemoForm.StrInput.value;<br>
//alert('in callGetLatestPoll');<br>
    var url = "http://localhost/ICG_webservice/Service.asmx/StoretoDB";<br>
    var params = "inputstring="+StrInput;<br>
    <br>
<br>
    http = getXMLHTTP();<br>
            <br>
<br>
    //http.responseText;<br>
   // http.overrideMimeType('text/xml');  <br> 
    http.onreadystatechange = function() {<br>
        //Call a function when the state changes.<br>
        if(http.readyState == 4) <br>
        {<br>
            if(isFirefox)<br>
               {<br>
               //xmlDoc = http.responseText;<br>
               //xmlDoc = http.responseText;<br>
               //http.overrideMimeType('text/xml');  <br>         
               //xmlDoc = http.responseXML;  <br>   
               //alert(http.responseXML);    <br>   
               //alert(http.status);<br>
               **fetchforfirefox()**<br>
               }<br>
            else if(http.status == 200)<br>
               {<br>
               //xmlDoc = http.responseXML;<br>
               xmlDoc=http.responseXML;<br>
               fetchlatestpoll()<br>
               }<br>
        }<br>
     }<br>
     http.open("POST", url, true);<br>
    //Send the proper header information along with the request<br>
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");<br>
    http.setRequestHeader("Content-length", params.length);<br>
    http.setRequestHeader("Connection", "close");<br>
<br>
    http.send(params);<br>
    //http.send();<br>
}<br>
<br>
function fetchlatestpoll()<br>
{debugger;<br>
        ////alert(xmlDoc.text);<br>
        alert(xmlDoc);<br>
        // code for reading and displaying data for internet explorer<br>
        b1 = xmlDoc.documentElement;<br>
        //alert(b1.childNodes.item(0).text);<br>
}<br>
<br>
function fetchforfirefox()<br>
{<br>
<br>
    alert('ff:step1');<br>
    //isFirefox=true;<br>
    //code for reading and displaying data for firefox<br>
     debugger;<br>
     alert('test');//works till here<br>
     alert(xmlDoc);**//just doesnt work in Firefox but works with venkman debugger**<br>
     var employees,i ;<br>
     employees = xmlDoc.getElementsByTagName("abc");<br>
    for(var i=0; i<employees.length; i++)<br>
     {<br>
        alert(employees[i].childNodes[0].nodeValue);<br>
    }<br>

  <br>
}<br>
</script><br>
</head><br>
<body>  <br>
<form id="DemoForm" name="DemoForm"><br>
<input type="text" name="StrInput" id="StrInput"/><br>
**<!--the button below is clicked to call webservice -->**<br>
<button onclick="callGetLatestPoll()">Save</button> <br>
</form><br>


******************webservice code*************************<br>
    [WebMethod]
    public System.Xml.XmlDataDocument  StoretoDB(string inputstring) {
        string returnVal = string.Empty;

        returnVal = dataHandlerObj.StoretoDB(inputstring);

        System.Xml.XmlDataDocument xmldoc = new System.Xml.XmlDataDocument();
        xmldoc.InnerXml = "<abc>"+returnVal+"</abc>";

        return xmldoc;
     }

I am trying to call a .net webservice from HTML page. This HTML page will be hosted on a different server. I using the following html code for this. The webservice code is below HTML code. This code runs just fine in IE and runs fine in Mozilla when debugging with venkman. But fails in normal execution in Firefox. I dont get anything in xmlDoc variable or http.responseXML or http.responseText or http.status.

I also get this error in error console "Error: xmlDoc is not defined Line: 104"

I guess the problem is that the anonymous callback function can't access anything outside.

enter code here



<script language="JavaScript">      

    var http =  null;<br>
    var isFirefox = false;<br>
    var StrInput;<br>
    var xmlDoc;<br>
   alert('Hi');<br>
function getXMLHTTP()<br>
{<br>
    var httpReq = null;<br>
<br>
    // Internet Explorer<br>
    try<br>
     {
     httpReq = new ActiveXObject("Msxml2.XMLHTTP");<br>
     }<br>
    catch (e)<br>
     {<br>
        try<br>
        {<br>
            httpReq = new ActiveXObject("Microsoft.XMLHTTP");<br>
        } <br>
        catch(oc)<br>
        {<br>
            httpReq = null;<br>
        }<br>
     }<br><br>
    // Firefox, Opera 8.0+, Safari..create object for webservice request<br>
    **if(!httpReq && typeof XMLHttpRequest != "undefined") <br>
        {<br>
            httpReq = new XMLHttpRequest();<br>
            isFirefox = true;<br>
        }**<br>
    return httpReq;<br>
}<br>
    <br>
function callGetLatestPoll()<br>
{<br>
debugger;<br>
    StrInput = document.DemoForm.StrInput.value;<br>
//alert('in callGetLatestPoll');<br>
    var url = "http://localhost/ICG_webservice/Service.asmx/StoretoDB";<br>
    var params = "inputstring="+StrInput;<br>
    <br>
<br>
    http = getXMLHTTP();<br>
            <br>
<br>
    //http.responseText;<br>
   // http.overrideMimeType('text/xml');  <br> 
    http.onreadystatechange = function() {<br>
        //Call a function when the state changes.<br>
        if(http.readyState == 4) <br>
        {<br>
            if(isFirefox)<br>
               {<br>
               //xmlDoc = http.responseText;<br>
               //xmlDoc = http.responseText;<br>
               //http.overrideMimeType('text/xml');  <br>         
               //xmlDoc = http.responseXML;  <br>   
               //alert(http.responseXML);    <br>   
               //alert(http.status);<br>
               **fetchforfirefox()**<br>
               }<br>
            else if(http.status == 200)<br>
               {<br>
               //xmlDoc = http.responseXML;<br>
               xmlDoc=http.responseXML;<br>
               fetchlatestpoll()<br>
               }<br>
        }<br>
     }<br>
     http.open("POST", url, true);<br>
    //Send the proper header information along with the request<br>
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");<br>
    http.setRequestHeader("Content-length", params.length);<br>
    http.setRequestHeader("Connection", "close");<br>
<br>
    http.send(params);<br>
    //http.send();<br>
}<br>
<br>
function fetchlatestpoll()<br>
{debugger;<br>
        ////alert(xmlDoc.text);<br>
        alert(xmlDoc);<br>
        // code for reading and displaying data for internet explorer<br>
        b1 = xmlDoc.documentElement;<br>
        //alert(b1.childNodes.item(0).text);<br>
}<br>
<br>
function fetchforfirefox()<br>
{<br>
<br>
    alert('ff:step1');<br>
    //isFirefox=true;<br>
    //code for reading and displaying data for firefox<br>
     debugger;<br>
     alert('test');//works till here<br>
     alert(xmlDoc);**//just doesnt work in Firefox but works with venkman debugger**<br>
     var employees,i ;<br>
     employees = xmlDoc.getElementsByTagName("abc");<br>
    for(var i=0; i<employees.length; i++)<br>
     {<br>
        alert(employees[i].childNodes[0].nodeValue);<br>
    }<br>

  <br>
}<br>
</script><br>
</head><br>
<body>  <br>
<form id="DemoForm" name="DemoForm"><br>
<input type="text" name="StrInput" id="StrInput"/><br>
**<!--the button below is clicked to call webservice -->**<br>
<button onclick="callGetLatestPoll()">Save</button> <br>
</form><br>


******************webservice code*************************<br>
    [WebMethod]
    public System.Xml.XmlDataDocument  StoretoDB(string inputstring) {
        string returnVal = string.Empty;

        returnVal = dataHandlerObj.StoretoDB(inputstring);

        System.Xml.XmlDataDocument xmldoc = new System.Xml.XmlDataDocument();
        xmldoc.InnerXml = "<abc>"+returnVal+"</abc>";

        return xmldoc;
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏见 2024-08-26 19:17:43

此 HTML 页面将托管在不同的服务器上。

不可能的。 JavaScript 无法访问来自不同域的内容。您可以让它在本地主机上工作,但部署后它将失败。

This HTML page will be hosted on a different server.

Impossible. Javascript can't access content from a different domain. You may get it to work on localhost, but upon deployment it will fail.

慢慢从新开始 2024-08-26 19:17:43

为了使用 XML Web 服务,两者都需要位于相同的域和端口上,或者您需要创建一个代理。尝试查看 JSON 这将允许您使用 JavaScript 来访问 Web 服务。

In order to consume an XML webservice, both will need to be on the same domain and port or you will need to create a proxy. Try looking into JSON which will allow you to use javascript to access the webservice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文