1

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;
     }
4

2 に答える 2

0

この HTML ページは別のサーバーでホストされます。

不可能。Javascript は、別のドメインのコンテンツにアクセスできません。localhost で動作するようになるかもしれませんが、展開すると失敗します。

于 2010-02-03T16:49:06.637 に答える
0

XML Web サービスを使用するには、両方が同じドメインとポート上にある必要があります。または、プロキシを作成する必要があります。JavaScript を使用して Web サービスにアクセスできるようにするJSONを調べてみてください。

于 2010-02-03T16:50:48.410 に答える