Git Product home page Git Product logo

Comments (7)

doedje avatar doedje commented on May 18, 2024

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...

from jquery.soap.

Deyine avatar Deyine commented on May 18, 2024

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 ...

from jquery.soap.

doedje avatar doedje commented on May 18, 2024

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!

from jquery.soap.

pixelfreak avatar pixelfreak commented on May 18, 2024

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'");
        };
    };

from jquery.soap.

doedje avatar doedje commented on May 18, 2024

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....

from jquery.soap.

pixelfreak avatar pixelfreak commented on May 18, 2024

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);

from jquery.soap.

doedje avatar doedje commented on May 18, 2024

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.... =]

from jquery.soap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.