doedje / jquery.soap

This script uses $.ajax to send a SOAP:Envelope. It can take XML DOM, XML string or JSON as input and the response can be returned as either XML DOM, XML string or JSON too.
352 stars 147 forks source link

Help needed in calling webservice #50

Closed rakesh1988 closed 10 years ago

rakesh1988 commented 10 years ago

Hello, I am developing a phonegap 3.X application and I am trying to call a webservice written in .NET. The problem is when the success call back is fired I get a blank value.

Code that I am using to call the webservice is as follows

$.soap({
                    url : 'http://some_ip/WebService/WebServiceAndroid.asmx?op=',
                    method : 'GetServerTime',
                    SOAPAction: "http://tempuri.org/GetServerTime",
                    data : {
                        Key : 'xyz',
                        Name : 'rakesh'
                    },
                    beforeSend : function(SOAPRequest) {
                        alert(SOAPRequest);
                    },
                    success : function(soapResponse) {
                        alert('success ' + soapResponse);
                    },
                    error : function(SOAPResponse) {
                        alert('failed '+SOAPResponse);
                    }
                });

beforeSend alert message seems to be fine with the message

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetServerTime>
<Key>xyz</Key>
<Name>rakesh</Name>
</GetServerTime>
</soap:Body>
</soap:Envelope>

success message gives me

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetServerTimeResponse xmlns="http://tempuri.org/">
<GetServerTimeResult/>
</GetServerTimeResponse>
</soap:Body>
</soap:Envelope>

What am i doing wrong here?

Also if I set url : 'http://some_ip/WebService/WebServiceAndroid.asmx?op=' to url : 'http://some_ip/WebService/WebServiceAndroid.asmx/', webservice fails. Is this normal?

image below shows the webservice request and response for soap 1.1 1

Result I am expecting is given below 1

Deyine commented 10 years ago

For me, using appendMethodToURL: false solve it

doedje commented 10 years ago

It looks to me that the url : 'http://some_ip/WebService/WebServiceAndroid.asmx/' is trying to find a directory rather than a file due to the trailing slash / but I don't know whether that is also the case with your .NET webservices...

Is the webservice giving expected results when calling it from another SOAP-client with the above SOAP-envelope? Do you have a way to check that?

Is your webservice expecting it's input parameters within the SOAP:Body? Sometimes it might also be looking for them in the querystring, like:

http://some_ip/WebService/WebServiceAndroid.asmx?op=GetServerTime&Key=xyz&Name=rakesh

Just my $0.02... As this issue is purely due to server implementation there is not very much I can do for you... Hope this helps you! =]

rakesh1988 commented 10 years ago

Thanks for all the replies. It was an implementation mistake. finally I was able to successfully get the data from the server using the following code

$.soap({
                    url : 'http://myip/WebService/WebServiceAndroid.asmx',
                    method : 'GetBranchMasterAsJson',
                    SOAPAction : "http://tempuri.org/GetBranchMasterAsJson",
                    namespaceURL : 'http://tempuri.org/',
                    appendMethodToURL : false,
                    envAttributes : {
                        'xmlns:xsi' : 'http://www.w3.org/2001/XMLSchema-instance',
                        'xmlns:xsd' : 'http://www.w3.org/2001/XMLSchema'
                    },
                    data : {
                        'Key' : 'xyz',
                        'ID' : 'abc'
                    },
                    beforeSend : function(SOAPRequest) {
                        alert(SOAPRequest);
                        console.log(SOAPRequest);
                    },
                    success : function(soapResponse) {

                        try {
                            alert('soapResponse.toString() ' + soapResponse.toString());

                            var json = $.xml2json(soapResponse.toXML());
                        } catch(ex) {
                            alert(ex);
                        }

                        console.log(json);
                        alert('0: success.. length of json is ' + json.length);
                    },
                    error : function(SOAPResponse) {
                        alert('1: failed ' + SOAPResponse);
                    }
                });

I get my result as follows in xml format

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetBranchMasterAsJsonResponse xmlns="http://tempuri.org/">
<GetBranchMasterAsJsonResult>
[{"BRANCHID":1.0,"BRANCHNAME":"abc","BANKID":1.0},
{"BRANCHID":2.0,"BRANCHNAME":"def","BANKID":1.0},
{"BRANCHID":3.0,"BRANCHNAME":"ghi","BANKID":2.0},
{"BRANCHID":4.0,"BRANCHNAME":"jkl","BANKID":3.0},
{"BRANCHID":5.0,"BRANCHNAME":"mno","BANKID":4.0},
{"BRANCHID":6.0,"BRANCHNAME":"pqr","BANKID":5.0}]
</GetBranchMasterAsJsonResult>
</GetBranchMasterAsJsonResponse>
</soap:Body>
</soap:Envelope>

when i try to convert it to json array using jquery.xml2json.js downloaded from www.fyneworks.com it always says that the data is not xml.. it fails at if(!xml) return {}; line 23 of jquery.xml2json.js

But when i debug using http://jsfiddle.net/rakesh1988/647L6/ I am getting right answer.

doedje commented 10 years ago

I tried to load your soap reponse with $.soap... did you try to use:

soapResponse.toJSON();

instead of:

 $.xml2json(soapResponse.toXML());

which is basically the same but keeps your part of the code a bit cleaner... =]

doedje commented 10 years ago

Ah!! I was looking at your fiddle and wondered how that was supposed to work because I could not find any call to the xml2json function anywhere in the code... But we are fooling ourselves here.... the stuff you see in the Results is just the soap envelope from the html panel interpreted as html... showing you only the json-formatted part of the envelope.... I removed the whole script all together and it still works, look:

http://jsfiddle.net/647L6/2/

trying to get the example to work on the console.log I discovered that xml2json does not like the JSON construct within the GetBranchMasterAsJsonResult in this way... When you replace the JSON with a text like foo it is all okay...

doedje commented 10 years ago

check out this fiddle:

http://jsfiddle.net/647L6/4/

and note that this works just fine... you are still looking at an implementation error somewhere...

console.log($.xml2json('<soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:body><getbranchmasterasjsonresponse xmlns="http://tempuri.org/"><getbranchmasterasjsonresult>[{"BRANCHID":1.0,"BRANCHNAME":"abc","BANKID":1.0},{"BRANCHID":2.0,"BRANCHNAME":"def","BANKID":1.0},{"BRANCHID":3.0,"BRANCHNAME":"ghi","BANKID":2.0},{"BRANCHID":4.0,"BRANCHNAME":"jkl","BANKID":3.0},{"BRANCHID":5.0,"BRANCHNAME":"mno","BANKID":4.0},{"BRANCHID":6.0,"BRANCHNAME":"pqr","BANKID":5.0}]</getbranchmasterasjsonresult></getbranchmasterasjsonresponse></soap:body></soap:envelope>'));
doedje commented 10 years ago

well, good luck again....! this is all the help I can give you for now.. the problem is not with $.soap or $.xml2json.... Keep hackin and let me know if you have any success..

doedje commented 10 years ago

I close this issue, hope you got it to work...