soapRequest未定义

时间:2016-12-12 06:45:45

标签: javascript html soap undefined-function

当我点击按钮时,我收到soapRequest is not defined错误。

如何解决此错误?

我的代码:

<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>

2 个答案:

答案 0 :(得分:0)

created

参考: - http://www.w3schools.com/jsref/event_onclick.asp

您需要在&#34; onclick&#34;上调用该功能。事件

答案 1 :(得分: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>