3

I have a login button in a jQuery Mobile application. When the login button is pressed a soap service is getting called using $.ajax() method. This works on browser and android phones but the control does not even go inside the $.ajax() in iOS devices. Here is my sample code.

var User = $("#txtUsername").val();
var Psw = $("#txtPwd").val();
var soapMessage = '<?xml version="1.0" encoding="utf-8"?>'
                    + '<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><ns0:UserLogin SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="urn:LoginSrvcVi"><userId xsi:type="xsd:string">'+User+'</userId><password xsi:type="xsd:string">'+Psw+'</password></ns0:UserLogin></SOAP-ENV:Body></SOAP-ENV:Envelope>';

$.ajax({
        url : myLoginUrl,
        type : "POST",
        username : User,
        password : Psw,
        dataType : "xml",
        data : soapMessage,
        contentType : "text/xml; charset=\"utf-8\"",

        success : function(data, textStatus, jqXHR) {
            debugger;
            $.mobile.loading('hide');
            console.log("data" + data);
            var x2js = new X2JS();
            var test = x2js.xml2json(data);                                                     
            debugger;
            if (test.Envelope.Body.searchUserLoginResponse.Response.messages.item != "Data Retrived Successfully") 
            {
                alert("Success");
            }
        },

        error : function(jqxhr) 
        {
            alert("Error");
        }

    });

Note : I tried the SOAP url to run in safari but it shows only a blank screen and no data. The chrome on android displays the XML structure.

Need Help. Thanks

abhhab
  • 253
  • 2
  • 6
  • 22
  • `alert("Success");` you have missed a `}` closing of `if` block. – Jai Apr 30 '15 at 12:33
  • @Jai That is a copy paste error and the if is closed in my code. Thanks. – abhhab May 04 '15 at 05:45
  • can you inspect your console what error message you got? – Nurdin May 04 '15 at 12:27
  • I am not getting this error on Browser or android phones. I am getting this error only on iOS devices. I have tried to give timeout of 50000 ms and then it goes into timeout error. When i try async : false, it gives me unauthorized error. – abhhab May 04 '15 at 12:59
  • @abhhab Tried adding `xhrFields` property `xhrFields: { withCredentials: true }` to `$.ajaxSettings` ?, possibly `crossDomain: true` ? ; see http://stackoverflow.com/questions/16689496/cross-domain-ajax-request-basic-authentication – guest271314 May 05 '15 at 17:19
  • Is your ajax call wrapped in both [jQuery's](http://api.jquery.com/ready/) `$(document).ready(handler);` and Phonegap's ["deviceready"](http://docs.phonegap.com/en/4.0.0/cordova_events_events.md.html#deviceready)? – eomer May 06 '15 at 09:24
  • @eomer No, it is not wrapped in any of the above but I had tried with phonegap's "deviceready" and yet it was not working. The same application is working on android without any jQuey's $(document).ready() and phonegap's "deviceready". – abhhab May 06 '15 at 09:31
  • Some webkit browsers load the same page differently even on different versions of Android. I have many times wrapped them in both. Usually starting with Phonegap and then jQuery. – eomer May 06 '15 at 09:33
  • I am using a soap service url for ajax call and only one url is not working. When I put the known value in any other soap message and pass its url in ajax, it works. But the url for login is not working. – abhhab May 06 '15 at 12:33
  • Have you inspected the request that comes from the iOS device? Are you sure it doesn't happen and doesn't reach the server? – Lanayx May 08 '15 at 17:44

2 Answers2

1

Try wrapping all of your code inside the 'document.ready' event callback like this:

$(document).ready(function () {
   // Your code here
});
1

If your request is working correctly on Android and Desktop browsers, but not on iOS then it sounds like there may be a bug in your app's iOS configuration for CORS. Given that you mentioned that you get an "unauthoerized error" when you try to make the call with async: false, it looks like your Domain Whitelist Configuration might be the source of the issue.

Make sure that you have added the domain to your whitelist configuration in the AppName/config.xml file (for iOS) as well as in the res/xml/config.xml file (for Android). Be sure to check out the documentation for more on how to add the proper configurations. As mentioned in this post, the config.xml file that you need to modify for iOS is the one in the project folder (AppName, in the above, refers to the project folder).

As you will see in the documentation, there are many different configurations that you can use to whitelist your domains, and some are more secure (by being more specific) than others. The following configuration can be used to grant access to all domains, but is very unspecific and thus less secure:

<access origin="*" />

Once you have ensured that your iOS whitelist configuration is up to date, set async: false and try the request again.

Community
  • 1
  • 1
Zachary Kniebel
  • 4,686
  • 3
  • 29
  • 53