2016-12-12 8 views
0

Wenn ich auf den Button klicke, bekomme ich einen soapRequest is not defined Fehler.soapRequest ist nicht definiert

Wie kann ich diesen Fehler beheben?

Mein Code:

<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

also Fehler, in der Linie? – nmnsud

+1

IDK, wenn Ihr Problem, aber 'alert xhr = new XDomainRequest(); 'keinen Sinn ergibt. –

+0

Wenn Sie Ihre Funktion wie diese 'var str = function createCORSRequest (Methode, URL) {' ... '}' definieren, müssen Sie sie als 'str (' ... ')' bezeichnen. Sie können es nicht als 'createCORSRequest (' ... ')' bezeichnen. Obwohl die 'str =' Zeile aussieht, sollte sie einen anderen Wert haben. Verwenden Sie [JSHint] (http://jshint.com/), um sofort Probleme mit Ihrem Code zu finden. – Xufox

Antwort

0

Versuchen Sie Folgendes:

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>