2016-12-12 57 views
0

當我點擊按鈕時,我得到一個soapRequest is not defined錯誤。soapRequest未定義

我該如何解決這個錯誤?

我的代碼:

<head> 
    <script type="text/javascript"> 
    var soapRequest = function() { 
     var str = //the soap request code 

      function createCORSRequest(method, url) { 
      var xhr = new XMLHttpRequest(); 
      if ("withCredentials" in xhr) { 
       xhr.open(method, url, false); 
      } else if (typeof XDomainRequest != "undefined") { 
       alert xhr = new XDomainRequest(); 
       xhr.open(method, url); 
      } else { 
       console.log("CORS not supported"); 
       alert("CORS not supported"); 
       xhr = null; 
      } 
      return xhr; 
      } //end of function createCORSRequest// 

     //calling the xhr 
     var xhr = createCORSRequest("POST", 
      "https://thelocalserver/therequest?wsdl"); 

     if (!xhr) { 
      console.log("XHR issue"); 
      return; 
     } 
     xhr.onload = function() { 
      var results = xhr.responseText; 
      console.log(results); 
      } //end of function onload 

     xhr.setRequestHeader('Content-Type', 'text/xml'); 
     xhr.send(str); 
     } //end of function soapRequest// 

    </script> 
</head> 

<body> 
    <form name="Demo" action="" method="post"> 
    <div> 
     <input type="button" id="API" value="Soap" onClick="soapRequest" /> 
    </div> 
    </form> 
</body> 
<html> 
+0

因此,錯誤在哪條線? – nmnsud

+1

IDK如果你的問題,但'alert xhr = new XDomainRequest();'沒有任何意義。 –

+0

如果你像這樣'var str = function createCORSRequest(method,url){'...'}'定義你的函數,那麼你需要把它稱爲'str('...')'。你不能把它稱爲'createCORSRequest('...')'。雖然'str ='行看起來應該保持不同的值。使用[JSHint](http://jshint.com/)立即查找代碼問題。 – Xufox

回答

0

嘗試以下操作:

的Javascript:

var soapRequest = function(){ 
    //the soap request code 
    var createCORSRequest = function (method, url) { 
     var xhr = new XMLHttpRequest(); 
     if ("withCredentials" in xhr) { 
      xhr.open(method, url, false); 
     } else if (typeof XDomainRequest != "undefined") { 
      var xhr = new XDomainRequest(); 
      xhr.open(method, url); 
     } else { 
      console.log("CORS not supported"); 
      alert("CORS not supported"); 
      xhr = null; 
     } 
     return xhr; 
    }//end of function createCORSRequest// 

    //calling the xhr 
    var xhr = createCORSRequest("POST", "https://thelocalserver/therequest?wsdl"); 

    if (!xhr){ 
     console.log("XHR issue"); 
     return; 
    } 

    xhr.onload = function(){ 
     var results = xhr.responseText; 
     console.log(results); 
    }//end of function onload 

    xhr.setRequestHeader('Content-Type', 'text/xml'); 
    xhr.send(str); 
}//end of function soapRequest// 

HTML:

<form name="Demo" action="" method="post"> 
    <div> 
     <input type="button" id="API" value="Soap" onClick="soapRequest()" /> 
    </div> 
</form>