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

phonegap, android only: Uncaught Error: Unexpected Content: null #66

Closed lorenzosfarra closed 9 years ago

lorenzosfarra commented 9 years ago

Hello! Here's the problem... The same code works if I run this on the iOS simulator, but on Android I have the following error:

11-17 18:22:27.318: D/CordovaLog(1778): file:///android_asset/www/js/jquery.soap.js: Line 392 : Uncaught Error: Unexpected Content: null

The code is very easy, as I'm only testing some features. So the code to include js files is the following:

`

    <script type="text/javascript" src="js/jquery.soap.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>`

in app.initialize I wait for the "deviceready" event to fire the following jquery soap call:

$.soap({
             url: 'http://<soapurl_here>/api/?wsdl',
             method: 'login',
             data: {
               username:'<user_here>',
               apiKey:'<api_key_here>'
             },

             success: function (s) {
               alert("success");
             },
             error: function (e) {
               alert("error");
             }

             });

Any hints?

Thanks in advance!

doedje commented 9 years ago

When you use:

file:///android_asset/www/js/jquery.soap.js

and

http://<soapurl_here>/api/?wsdl

You will indeed get a

Uncaught Error: Unexpected Content: null

Due to same-origin policy... Try not to use file:// but only http:// for your test-setup. Based on the info you provided my guess is there is your problem, good luck!

lorenzosfarra commented 9 years ago

Hi, first of all thanks for your reply. It's quite strange because the same-origin policy is "solved" by phonegap using the concept of whitelist. I am using the <access origin="*" /> setting, both for android and iOS (and on iOS works), and the code is equivalent. This is why I am completely stuck, asking for your hints.

[UPDATE] And it seems that:

Luckily since your PhoneGap application is running off of the file:// protocol it isn't limited by the same origin policy. This means we can request data using XmlHttpRequest from any domain.

as stated in different documents like this blog post

Thanks!

doedje commented 9 years ago

Well in that case I suspect The problem to be with phonegap, The error is clearly pointing in the direction of same origin policy problems...

lorenzosfarra commented 9 years ago

Yep, at this point I agree...totally something not related to this library, thanks for your answers anyway.

doedje commented 9 years ago

Unfortunately I have no experience with phonegap, so I won't be able to help you any further... When you do find some interesting information, please consider sharing it here, you might be helping other phonegap/jquery.soap programmers with your findings! =]

good luck!

Haibarbe-Kreativ commented 9 years ago

Hi there, I recently have done an phonegap iOS and Android App for an client of mine that is now in stores and uses jquery.soap to contact a SOA-Webservice. It was a little bit of pain at first but then works out fine. I will give you some code snippets of mine, maybe that helps.

var Network = {
soapRequestInit: function () {
    $.soap({
        url: 'http://www. [ yourURLHere ] .asmx',
        soap12: true,
        appendMethodToURL: false,
        namespaceQualifier: '[ yourNamespaceQualifier ]',
        namespaceURL: 'http://www. [ yourURLHere ] .asmx',
    });
},
logIn: function (password, name, saveit) {
$.soap({
        SOAPAction: 'http://www. [ yourURLHere ] .asmx/LogIn',
        method: 'LogIn',
        data: {
            userName: name,
            password: password
        },
        beforeSend: function (obj) {
            console.log("soap-env: " + obj.toString());
        },
        success: function (soapResponse) {
            //...do the relevant stuff
            //...

        },
        error: function (SOAPResponse) {
            console.log("Error: " + SOAPResponse.statusText);
            $.mobile.loading('hide');
            //...do the relevant stuff
            //...

        }
    });
},
//...other methods of the Network-Object 
//...

}

I did not have to whitelist anything, there where just a couple of headers that had to be set at the webservice for output. These where the following: Access-Control-Allow-Origin: * Access-Control-Allow-Headers : SOAPAction und/oder Content-Type

If these headers are set on the response it should work fine.

lorenzosfarra commented 9 years ago

Thanks for the reply! I will take inspiration from the above code to try something in the one I have, thanks.