Reputation: 33
I'm having a problem with trying to set up a working web service in my application I'm making using Phonegap. I need to get data from an existing web service. I found that by using a simple ajax request, this should be working. Am I not using the ajax request correctly?
The web service I'm trying to call can be found here: http://ws.swinggift.com/SGServices.asmx
EDIT: I tested it on http://wsdlbrowser.com/ and I'm getting my xml file back, how does this site work ?
I'm working in the ripple emulator so I have a cross domain proxy. I'm suspecting that my request header may be off ?
error that I'm getting: Failed to load resource: the server responded with a status of 400 (Bad Request) (10:26:26:851 | error, network) at https://rippleapi.herokuapp.com/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=http%3A//ws.swinggift.com/SGServices.asmx%3Fop%3DGetVouchers
(I can't make my logon code public)
my test html file:
<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>
Thanks!
EDIT: I don't know if it helps but if I do a 'GET' with the this code, I get the webpage in HTML format if I ask for the 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>
Upvotes: 2
Views: 1991
Reputation: 708
Try setting processData: false
. This flag is true by default and jQuery is converting your XML to string.
$.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
})
Upvotes: 1