I have problems parsing response from SOAP sever with jQuery. I want to convert XML response to array because there are multiple rows of data as you can see below.
This is request:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="/">
<soap:Body>
<GetWorkPos xmlns="http://localhost/apps">
<id>int</id>
</GetWorkPos>
</soap:Body>
</soap:Envelope>
And this is response:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="/">
<soap:Body>
<GetWorkPosResponse xmlns="">
<GetWorkPosResult>
<GetWorkPos>
<ProductId>string</ProductId>
<Product>string</Product>
<quantity>decimal</quantity>
<Em>string</Em>
<type>string</type>
</GetWorkPos>
<GetWorkPos>
<ProductId>string</ProductId>
<Product>string</Product>
<Quantity>decimal</Quantity>
<Em>string</Em>
<Type>string</Type>
</GetWorkPos>
</GetWorkPosResult>
</GetWorkPosResponse>
</soap:Body>
</soap:Envelope>
And this is my code:
$(document).ready(function () {
$("#send").click(function (event) {
var wsUrl = "http://localhost/Service.asmx";
var soapRequest =
'<?xml version="1.0" encoding="utf-8"?> \
<soap:Envelope xmlns:xsi="" \
xmlns:xsd="" \
xmlns:soap="/"> \
<soap:Body> \
<GetWorkPos xmlns=""> \
<id>' + $("#id").val() + '</id> \
</GetWorkPos> \
</soap:Body> \
</soap:Envelope>';
console.log(soapRequest);
$.ajax({
type: "post",
url: wsUrl,
contentType: "text/xml",
dataType: "xml",
data: soapRequest,
success: processSuccess,
error: processError
});
});
});
function processSuccess(data, status, req, xml, xmlHttpRequest, responseXML) {
$(req.responseXML)
.find('GetWorkPosResult')
.each(function(){
var id = $(this).find('ProductId').text();
console.log(id);
});
}
function processError(data, status, req) {
alert(req.responseText + " " + status);
console.log(data);
console.log(status);
console.log(req);
}
I have problems parsing response from SOAP sever with jQuery. I want to convert XML response to array because there are multiple rows of data as you can see below.
This is request:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3/2001/XMLSchema-instance" xmlns:xsd="http://www.w3/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap/soap/envelope/">
<soap:Body>
<GetWorkPos xmlns="http://localhost/apps">
<id>int</id>
</GetWorkPos>
</soap:Body>
</soap:Envelope>
And this is response:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3/2001/XMLSchema-instance" xmlns:xsd="http://www.w3/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap/soap/envelope/">
<soap:Body>
<GetWorkPosResponse xmlns="http://localhost./apps">
<GetWorkPosResult>
<GetWorkPos>
<ProductId>string</ProductId>
<Product>string</Product>
<quantity>decimal</quantity>
<Em>string</Em>
<type>string</type>
</GetWorkPos>
<GetWorkPos>
<ProductId>string</ProductId>
<Product>string</Product>
<Quantity>decimal</Quantity>
<Em>string</Em>
<Type>string</Type>
</GetWorkPos>
</GetWorkPosResult>
</GetWorkPosResponse>
</soap:Body>
</soap:Envelope>
And this is my code:
$(document).ready(function () {
$("#send").click(function (event) {
var wsUrl = "http://localhost/Service.asmx";
var soapRequest =
'<?xml version="1.0" encoding="utf-8"?> \
<soap:Envelope xmlns:xsi="http://www.w3/2001/XMLSchema-instance" \
xmlns:xsd="http://www.w3/2001/XMLSchema" \
xmlns:soap="http://schemas.xmlsoap/soap/envelope/"> \
<soap:Body> \
<GetWorkPos xmlns="http://localhost./apps"> \
<id>' + $("#id").val() + '</id> \
</GetWorkPos> \
</soap:Body> \
</soap:Envelope>';
console.log(soapRequest);
$.ajax({
type: "post",
url: wsUrl,
contentType: "text/xml",
dataType: "xml",
data: soapRequest,
success: processSuccess,
error: processError
});
});
});
function processSuccess(data, status, req, xml, xmlHttpRequest, responseXML) {
$(req.responseXML)
.find('GetWorkPosResult')
.each(function(){
var id = $(this).find('ProductId').text();
console.log(id);
});
}
function processError(data, status, req) {
alert(req.responseText + " " + status);
console.log(data);
console.log(status);
console.log(req);
}
Share
Improve this question
asked Apr 28, 2012 at 14:56
SoriyyxSoriyyx
8933 gold badges16 silver badges33 bronze badges
5
- 1 I cannot convert it to array. – Soriyyx Commented Apr 28, 2012 at 15:01
- Just append the "id" values to an array; the code you posted makes no attempt to do so. – Pointy Commented Apr 28, 2012 at 15:06
- In result I get 100010011002 instead of 1000, 1001, 1002 – Soriyyx Commented Apr 28, 2012 at 15:11
- 2 Well if you don't post the code you're trying to use to create the array, nobody can help you. – Pointy Commented Apr 28, 2012 at 15:14
- I just add var id = new Array; – Soriyyx Commented Apr 28, 2012 at 15:18
1 Answer
Reset to default 10http://jsbin./uwirux/edit#javascript,html
var myObj = new Array();
$(req.responseXML)
.find('GetWorkPosResult').find('GetWorkPos')
.each(function(){
myObj.push($(this)); // Should't use .text() because you'll lose the ability to use .find('tagName')
});
for(var i = 0; i<myObj.length;i++){
var x = myObj[i].find('ProductId').text();
var y = myObj[i]find('Product').text();
}
or
$(myObj).each(function(){
var x = $(this).find('ProductId').text();
var y = $(this).find('Product').text();
});