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 148 forks source link

How to correctly require jquery.soap module in node js app? #88

Closed gamecubate closed 5 years ago

gamecubate commented 8 years ago

How does one use jquery.soap inside a node js app? I tried this:

var $ = require('jquery');
var jQuery = $;
require('jquery.soap');

and got this:

node_modules/jquery.soap/jquery.soap.js:669
})(jQuery);
   ^
ReferenceError: jQuery is not defined

Thanks in advance.

doedje commented 8 years ago

Sadly I am quite unfamiliar with node.js but from what I read about it so far I understand that I should do a little rewriting of $.soap to make it work easily.... I will look into that.

While waiting for that you could try:

var $ = global.jQuery = require('jquery');
require('jquery.soap');

Not sure whether that should work tho.... It could be you should change the line 669 of $.soap to:

})(global.jQuery);
doedje commented 8 years ago

The waiting is over..... At least, I only need somebody to test this code in nodejs, it is still working fine in the browser... So would you be so kind as to test this version of $.soap in your nodejs setup and let me know if it works properly?

You should now be able to do:

var $ = require('jquery');
require('jquery.soap');
$.soap({ .... });

And since $.soap is requiring jquery by itself now and it is also exporting itself, you should also be able to do:

var $soap = require('jquery.soap');
$soap({ ... });

Which could be useful if $.soap is going to be the only thing you might ever want to do with jquery...

Please let me know the results of your tests so I can make this the 1.6.8 version of $.soap =]

gamecubate commented 8 years ago

I copied your changed jquery-soap.js file in the root directory of my project (as opposed to inside the node_modules/jquery.soap subdirectory). Then I required it this way:

var $ = require('jquery');
var $soap = require('./jquery.soap');

Calling node myapp.js, I get this error message:

    $.extend(config, globalConfig, options);
      ^
TypeError: undefined is not a function

I then tried another test, replacing node_modules/jquery.soap/jquery.soap.js with your changed version. Changing require calls to:

var $ = require('jquery');
var $soap = require('jquery.soap');

Launching again, I get the same error message.

doedje commented 8 years ago

Thanx for reporting your findings, I think I'll have to install node and try this by myself... In the meantime you can still try the suggestions from my first reply. If it works that way you can focus on building your own project, while I sort out a nice solution for this.....

doedje commented 8 years ago

I am running some test now and I think I might educate myself about scopes in nodejs, as far as I can tell now (from just playing with it for a couple of minutes) scopes in nodejs are not the same as in the browserworld......

gamecubate commented 8 years ago

My instinct would be to agree with you but I would hard pressed to list the specific differences. :]

gamecubate commented 8 years ago

So I did as per your first response:

var $ = global.jQuery = require('jquery');
require('jquery.soap');

after having changed the last line of (your original, unmodified) jquery.soap.js to:

})(global.jQuery);

and no cigar:

module.js:338
    throw err;
          ^
Error: Cannot find module 'jquery.soap'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (~alexr/TESTS/SOAP/login-jquery-soap.js:4:1)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
doedje commented 8 years ago

still working on this, but I put this on long term... Need to find time, busy schedule....

gamecubate commented 8 years ago

That's okay. (Canadian) elections are now over. I had found another way (with the help of some PHP scripting) to retrieve the SOAP data I needed.

doedje commented 8 years ago

Anyone that can help me further fix this issue is more than welcome to comment, commit, send pull requests etc... Because I am totally stuck on this one.... :(

ghost commented 8 years ago

@doedje, use node-soap. jquery.soap is more for client-stuff, if you want to create a server or something I really recommend you node-soap.

however, I'm using React right now, the above method worked for me, but is kinda ugly, so let me know when this PR is ready, @gamecubate. node-soap is not working with webpack (sucks)

gamecubate commented 8 years ago

@MrOutis I stopped using webpack a long time ago so no problem. As for SOAP, I am staying away from that too -- it's too verbose for my taste; I choose JSON output APIs whenever I can -- so I'll stash that away until the next project that needs -- please, no! -- it.

@doedje This is no longer an issue for me so please feel free to close it.

doedje commented 8 years ago

@gamecubate: I will leave it open hoping for someone to drop by and make it work in node.js as well.... I won't need it, but it could be useful to do client-stuff from node.js

It was never my choice to work with soap, I totally agree with you that rest-api's returning json are preferable...

divinocodino commented 8 years ago

I tried this,

import $ from 'jquery';
require('jquery.soap');

and on the jquery.soap.js


var SOAPModule = (function (factory) {
  if(typeof module === "object" && typeof module.exports === "object") {
    factory(require("jquery"));
  } else {
    factory(jQuery);
  }
}(function($) {

it worked, is it possible to add this to new version ?

divinocodino commented 8 years ago

Also this works as well

import $ from 'jquery';
import $soap from 'jquery.soap';

with


var SOAPModule = (function (factory) {
  if(typeof module === "object" && typeof module.exports === "object") {
    factory(require("jquery"));
  } else {
    factory(jQuery);
  }
}(function($) {
    var enableLogging;
    var globalConfig = { // this setup once, defaults go here
        appendMethodToURL: true,
        async: true,
        enableLogging: false,
        noPrefix: false,
        soap12: false
    };

    $.soap = SOAPFunction;

    if(typeof module === "object" && typeof module.exports === "object") {
        module.exports = SOAPFunction   
    }

    function SOAPFunction(options) {
        var config = {};
doedje commented 8 years ago

I was away for holidays, so sorry for the late reply. Hopefully you can send me a pull request so I can test it and add it to the 1.6.9 release.

doedje commented 8 years ago

I already found your fork and pulled your changes into my local dev-branche... Review the code now... THNX!

doedje commented 8 years ago

The proposed code changes do Not make query.soap work in a server-side nodejs environment..... Still get the same error.

$.extend is not a function
Sergiodiaz53 commented 7 years ago

Is this fixed? is there any way to call $.soap from nodejs? Thanks in advance.

doedje commented 7 years ago

I am afraid this is still not working.... My experience with nodejs is too limited to be able to solve this issue myself. I did my best, but for now I'll just hope someone to come along and finds us a solution... Any change you are our guy?? ;)

Sergiodiaz53 commented 7 years ago

Well, I will try to explain what I did. I think is not the correct solution but It works:

I have commented the jquery.soap file (attached) to explain more or less what I have done. Then, in your app.js node file you should do this:

var jsdom = require("jsdom").jsdom;
var doc = jsdom();
var window = doc.defaultView;
var $ = require("jquery")(window);
var soap = require('./node_modules/jquery-soap/lib/jquery.soap.js');

and now you can use soap.soap() as $.soap:

soap.soap({
    url: 'MyWSDLurl',
    appendMethodToURL: false,
    SOAPAction: '',
    crossDomain: true,

    HTTPHeaders: {},

    error: function(soapResponse) {
    console.log(SOAPResponse.toString());
    },
});

I don't know if update this as a new branch or leave it here. Hope it helps someone!!

jquery.soap.txt

doedje commented 5 years ago

closing this for now, feel free to reopen when improvements is there....