Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing response with promise #49

Closed
Deyine opened this issue May 15, 2014 · 7 comments
Closed

Parsing response with promise #49

Deyine opened this issue May 15, 2014 · 7 comments

Comments

@Deyine
Copy link

Deyine commented May 15, 2014

first of all, thanks for this very useful lib.

I'm using it with angularJS so my soap service returns a promise to controller.

How can i parse response in done status

This is service

.factory('contactService', ['$http', 'CONFIG',
    function ($http, CONFIG) {
      return {
        findContacts: function () {
          return $.soap({
          url : CONFIG.contact.url,
          method : CONFIG.contact.find,
          namespaceURL : CONFIG.contact.namespace,
          data: {}
         });
      },

And controller

contactService.findContacts()
    .done(function(data, textStatus, jqXHR) {
        var response = new SOAPResponse(textStatus, jqXHR);
        console.log($.parseXML(response.toString()).firstChild);
    });

Error SOAPResponse is undefined

@doedje
Copy link
Owner

doedje commented May 16, 2014

It took me a while to get my mind wrapped around this one, but I do understand now...
The SOAPResponse object is only available within the success and error functions of the $.soap options. When using promises you get back (data, textStatus, jqXHR) so you need to do something like:

// untested code:
contactService.findContacts()
    .done(function(data, textStatus, jqXHR) {
        console.log($.parseXML(data).firstChild);
    });

Hope this helps...

@Deyine
Copy link
Author

Deyine commented May 16, 2014

Thanks @doedje,

This is what i'm doing now to get SOAP result as JSON

contactService.findContacts()
  .done(function(data, textStatus, jqXHR) {
    // TODO use JsonPath to avoid using multiple firstChild
    // Depth = data(document).firstChild(Soap:Envelope).firstChild(Soap:Body).firstChild(Soap:Body).firstChild(Soap:Response)
    var jsonResponse = $.xml2json(data.firstChild.firstChild.firstChild);
    console.log(jsonResponse.GetContactsResponse.ListContacts);
  });

Very ugly, specially when I make multiple calls of firstChild to get the response ...

@doedje
Copy link
Owner

doedje commented May 19, 2014

You could also use jquery to parse the XML.
With the following SOAPResonse:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns:return xmlns:ns="http://localhost/">
            <ns:status>STATUS_OK</ns:status>
            <ns:message>FAKE SOAP RESPONSE!</ns:message>
        </ns:return>
    </soap:Body>
</soap:Envelope>

You could do like this:

contactService.findContacts()
  .done(function(data, textStatus, jqXHR) {
    console.log($(data).find('ns\\:status').text());
  });

In your case:

   var jsonResponse = $.xml2json($(data).find('GetContactsResponse').html());

Just to let you know! Good luck with coding!

@doedje doedje closed this as completed May 19, 2014
@pixelfreak
Copy link

This is old, but looking at how SoapResponse does it, it's using the xhr response instead of data, not sure if that makes any difference

var SOAPResponse = function(status, xhr) {
        this.typeOf = "SOAPResponse";
        this.status = status;
        this.headers = xhr.getAllResponseHeaders().split('\n');
        this.httpCode = xhr.status;
        this.httpText = xhr.statusText;
        this.content = (xhr.responseXML === undefined) ? xhr.responseText : xhr.responseXML;
        this.toString = function(){
            if (typeof this.content === 'string') {
                return this.content;
            }
            if ($.isXMLDoc(this.content)) {
                return SOAPTool.dom2string(this.content);
            }
            throw new Error("Unexpected Content: " + $.type(this.content));
        };
        this.toXML = function(){
            if ($.isXMLDoc(this.content)) {
                return this.content;
            }
            return $.parseXML(this.content);
        };
        this.toJSON = function(){
            if ($.xml2json) {
                return $.xml2json(this.content);
            }
            warn("jQuery.soap: Missing JQuery Plugin 'xml2json'");
        };
    };

@doedje
Copy link
Owner

doedje commented May 11, 2015

the difference is this: you can either use the success option or the promise function .done to deal with the response, just a matter of personal preference I guess:

$.soap({
  // blah, blah, all kind of options here... like url, data, etc...
  success: function(SOAPResponse) {
    // here you get back the SOAPResponse object you copied above
    // with it's properties and functions, like toJSON() for instance...
    console.log(SOAPResponse.toJSON());
  }
});

the OP of this issue wanted to use the Promise returned by $.soap, which is basically the one that is returned by $.ajax:

$.soap({
  // blah, blah, all kind of options here... like url, data, etc...
}).done(function(data, textStatus, jqXHR) {
  // here you get back `data, textStatus, jqXHR` so you can do:
  console.log(data);
});

Hope that clears the confusion a bit....

@pixelfreak
Copy link

I think you misunderstood. I was referring to the difference between:

$.xml2json(data);

vs

var content = (xhr.responseXML === undefined) ? xhr.responseText : xhr.responseXML;
$.xml2json(content);

@doedje
Copy link
Owner

doedje commented May 11, 2015

Ah I see, I misunderstood indeed, I thought your question was somehow related to this issue, but if it does I don't understand how...

But the difference is unclear to me. $.soap is open source project with a handful of people collaborating to it in the past years. This got introduced at some point around the 0.10.0 version by Zach if I recall this correctly, I hope it was for the best.... =]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants