Reputation: 253
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
Upvotes: 3
Views: 645
Reputation: 4774
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.
Upvotes: 1
Reputation: 148
Try wrapping all of your code inside the 'document.ready' event callback like this:
$(document).ready(function () {
// Your code here
});
Upvotes: 1