2014-04-24 44 views
2

我在尝试使用Phonegap在我的应用程序中设置工作的Web服务时出现问题。我需要从现有的Web服务获取数据。我发现通过使用简单的ajax请求,这应该是工作。我没有正确使用ajax请求吗?由ajax在phonegap中的SOAP web服务调用错误

我试图调用Web服务可以在这里找到:http://ws.swinggift.com/SGServices.asmx

编辑:我测试http://wsdlbrowser.com/,我让我的XML文件回来,怎么做这个网站的工作?

我正在涟漪模拟器中工作,所以我有一个跨域代理。 我怀疑我的请求标题可能已关闭?

错误我得到: 无法加载资源:服务器400(错误请求)的状态(26:26:10 851 |错误,网络)回应 在https://rippleapi.herokuapp.com/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=http%3A//ws.swinggift.com/SGServices.asmx%3Fop%3DGetVouchers

(我不能让我的注册码公众)

我的测试HTML文件:

<html> 
    <head> 
     <title>Calling Web Service from jQuery</title> 
     <script type="text/javascript"   src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"> 
</script> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       $("#btnCallWebService").click(function(event) { 
        var wsUrl = "http://ws.swinggift.com/SGServices.asmx?op=GetVouchers"; 

        var soapRequest = 
          '<?xml version="1.0" encoding="utf-8"?>' + 
          '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + 
          '<soap:Body>' + 
          '<GetVouchers xmlns="http://tempuri.org/">' + 
          '<logoncode>ICantGiveYouThis</logoncode>' + 
          '</GetVouchers>' + 
          '</soap:Body>' + 
          '</soap:Envelope>'; 

        $.ajax({ 
         type: "POST", 
         url: wsUrl, 
         contentType: "text/xml; charset=utf-8", 
         dataType: "xml", 
         crossDomain: true, 
         data: soapRequest, 
         beforeSend: function(XMLHttpRequest) { 
          XMLHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
          XMLHttpRequest.setRequestHeader("SOAPAction", "http://tempuri.org/GetVouchers"); 
          XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*"); 
         }, 
         success: processSuccess, 
         error: processError 
        }); 
       }); 
      }); 

      function processSuccess(data, status, req) { 
       if (status === "success") 
        $("#response").text($(req.responseXML)); 
      } 

      function processError(data, status, req) { 
       console.log(req.responseText + " " + status); 
      } 

     </script> 
    </head> 
    <body> 
     <h3> 
      Calling Web Services with jQuery/AJAX 
     </h3> 
     <input id="btnCallWebService" value="Call web service" type="button" /> 
     <div id="response" > 
     </div> 
    </body> 
</html> 

谢谢!

编辑: 我不知道是否有帮助,但如果我做一个“GET”用这个代码,我得到的HTML格式的网页,如果我问这个responseText

<html> 
    <head> 
     <title>SOAP JavaScript Client Test</title> 

     <!-- jQuery/jQueryMobile Scripts --> 
      <script src="js/jquery-1.9.1.min.js"></script> 
      <script src="js/jquery.mobile-1.3.1.min.js"></script> 

     <script type="text/javascript"> 
      function soap() { 
       var xmlhttp = new XMLHttpRequest(); 
       xmlhttp.open('GET', 'http://ws.swinggift.com/SGServices.asmx?op=GetVouchers', true); 

       // build SOAP request 
       var sr = 
        '<?xml version="1.0" encoding="utf-8"?>' + 
           '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + 
           '<soap:Body>' + 
           '<GetVouchers xmlns="http://tempuri.org/">' + 
           '<logoncode>something</logoncode>' + 
           '</GetVouchers>' + 
           '</soap:Body>' + 
           '</soap:Envelope>'; 

       xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200) { 
          console.log('done' + xmlhttp.responseText); 
          $("#response").text($(xmlhttp.responseXML)); 
         } 
        }; 

       // Send the POST request 
       xmlhttp.setRequestHeader('Content-Type', 'text/xml'); 
       xmlhttp.setRequestHeader("Accept", "application/xml, text/xml, */*"); 
       xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/GetVouchers"); 
       xmlhttp.send(sr); 
       // send request 
       // ... 
      } 
     </script> 
    </head> 
    <body> 
     <form name="Demo" action="" method="post"> 
      <div> 
       <input type="button" value="Soap" onclick="soap();" /> 
       <div id="response" > 
      </div> 
      </div> 
     </form> 
    </body> 
    </html> 
+0

您需要修改Ripple proxy.js。看到这篇文章:http://stackoverflow.com/questions/29222701/400-error-calling-wcf-web-service-from-ripple-emulator –

回答

1

这是一个模拟器的问题...上面的代码现在的工作。

1

尝试设置processData: false。这个标志默认是true,jQuery将你的XML转换为字符串。

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

$.ajax({ 
        type: "POST", 
        url: wsUrl, 
        contentType: "text/xml; charset=utf-8", 
        dataType: "xml", 
        crossDomain: true, 
        data: soapRequest, 
        processData: false, 
        beforeSend: function(XMLHttpRequest) { 
         XMLHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
         XMLHttpRequest.setRequestHeader("SOAPAction", "http://tempuri.org/GetVouchers"); 
         XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*"); 
        }, 
        success: processSuccess, 
        error: processError 
       }) 
+0

感谢您的答案,但不幸的是,我仍然得到相同的错误。尽管如此,我会继续努力。 – schutyser