android - Ajax json cross domain request not working with cordova app -
i doing cordova android app. in want user login functionality authenticated rest api. in mobile device giving ajax error code '0' , thrownerror blank. have attached device desktop system testing app.
while in desktop browser working fine. have used below code,
apiurl = "http://xxx.xxx.xxx.xxx/restapi/"; $.ajax({ url: apiurl+'user-signin/login', type: 'post', datatype :'json' crossdomain: true, beforesend:function(){ $.mobile.loading( 'show' ); }, complete: function() { $.mobile.loading( 'hide' ) }, success: function (res) { alert(res.status); if(res.status == "success") { goinsert(res.result.user_id, res.result.user_name, res.result.user_password); window.location="dashboard.html"; } else if(res.status == "failed") { alert(res.msg); //alert(message[res.msg]); } }, error: function(xhr, textstatus, thrownerror) { alert(xhr.status+":"+textstatus+":"+thrownerror); navigator.app.exitapp(); }, data: new formdata($('#loginfrm')[0]), cache: false, contenttype: false, processdata: false });
i have added "header("access-control-allow-origin: *"); " in rest api.
i have used datatype "jsonp", not working me.
can me in this?
if using cordova 5 need @ content security policy meta tag in index.html - default it's not going allow unless amend include api's url.
try modifying meta tag (or add 1 if not present) this:
<meta http-equiv="content-security-policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src http://xxx.xxx.xxx.xxx/">
for deeper dive check out http://moduscreate.com/cordova-5-ios-9-security-policy-changes/
Comments
Post a Comment