Can't access php file from other domain - android

I written a android mobile application in JQuery Mobile and PhoneGap in Eclipse. In the application I am calling a jquery ajax to load list of data from other domain.
My jquery ajax call code is:
$.ajax({
type: "POST",
url: WEBSERVICE_URL,
async: false,
data: dataString,
dataType: 'json',
crossDomain: true,
success: function(data) {
loginData = new Object(data);
hideActivityIndigator();
if(loginData.success == "true"){
$.mobile.changePage("#selectionScreen", "slide", false, true);
} else {
$("#message_ajax").html("Invalid UserName/Password.");
}
},
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
hideActivityIndigator();
}
});
The variable "WEBSERVICE_URL" has a other domain php service url.
On executing above code on "pageview" event I am getting following error
Error: NETWORK_ERR: XMLHttpRequest Exception 101
Any solution is there to access service from other domain in JQuery Mobile + PhoneGap...?

I think this might solve your problem.

If you are using Phonegap/Cordova you should be able to call cross-domain web-services.. are you using an emulator or a phone?
As for emulator I would recommend Ripple, you can add it as a Chrome extension or Download the standalone version

Related

Cordova doesn't add data to URL (AJAX)

I'm sending an Jquery.Ajax request to a server which is hosting a .asmx web service.
jQuery.ajax({
type: 'GET',
url: 'http://IP-destination/myFolder/WebService.asmx/LogonUser',
cache: 'true',
data: {
'username':get_cookie('username'),
'password':get_cookie('password'),
'environment':get_cookie('environment')
},
dataType: 'jsonp',
beforeSend: function(){$.mobile.loading('show', 'a', 'Laddar...', false); alert("go");},
error: function(a, b, c){
$.mobile.loading('hide','a');
alert(a + b+ c);
},
success: function(response){
$.mobile.loading('hide','a');
alert("success");
self.loginUser_cb(response);
}
});
While sending data through my browser, emulator or "Legacy Hybrid build (Intel XDK) for android" everything works great.
But when I use Cordova (still android, works on IOS) the request get an "500 Internal Server Error".
It seems like my URL is missing my data parameters:
http://oi60.tinypic.com/21jvb5y.jpg
When I use my browser the URL is correct with the data parameter in the end of the url: /LogonUser?callback=jQuery19105695250731240461_1425028837473&username=MyUserName&password=Secret&environment=Dev
My question is: What could possibly cause this in Cordova Android but work great
in the emulator/ios/Hybrid Legacy build? The URL seem to be cut short.
I'd look to simplify things as much as possible and see if the get_cookie portion is failing for you. Try removing that and using static values. If it works, it means your cookies may not exist as you think they should and you should write logic to handle that.

Android WebView AJAX Request

I'am develoing a VB.NET MVC4 Web App using Android's webView to run it.
I have the next problem: When i make an ajax PUT request to my own controllers method, i recieve a 404 status code.
In the other hand, when i make a get or post petition, works fine. ¿Any idea?
$.ajax({
type: 'PUT',
url: 'myurl',
data: data,
success: function (json, textStatus) {
alert(json);
},
error: function (jqXHR) {
alert('error');
}
});
jqXHR.readystate = 4
jqXHR.message = server error file or directory not found
jqXHR,status: 404
EDIT: IIS is configured to work with PUT and DELETE and thanks to that it works correctly in the browser but not with the Androids WebView.
Your code is missing a quote url: 'myurl needs a ' on the end
$.ajax({
type: 'PUT',
url: 'myurl',
data: data,
success: function (json, textStatus) {
alert(json);
},
error: function (jqXHR) {
alert('error');
}
});
This might be an error in your example but that's all we can see to correct the other things to check is IIS Setup of the site does is it set to allow PUT, DELETE ect
GET & POST are standard and are enabled by default.
So please follow the answer to this question:
How do I enable HTTP PUT and DELETE for ASP.NET MVC in IIS?

How to load data from another website using jquery mobile application for android ?

I want to build an app using jquery mobile and phonegap. My problem is When I try to load json data using $.ajax() method it works fine in local host but when i try this at live hosting it results nothing. Here is my code.
$.ajax({
type: "GET",
url: "http://rahultest.herobo.com/rbbank/login.php",
crossDomain: true,
data: {id: 'rahul123', pswd:'xxxxx'},
dataType:"json",
success:function(data){
if(data.status=="connected"){
user_id=data.id;
$.mobile.navigate("#menu");
}
else{
alert("User ID and Password is incorrect");
}
},
error: function(){alert("Could not connect");}
});
});
Have you added the domain to Phonegaps whitelist?

PhoneGap, jQuery getJSON and CORS

I'm developing a mobile application based on PhoneGap. I need to use AJAX, I'm working with jQuery.
I'm trying to get a JSON from a WS on PHP with CORS, but I get an error, and the error message is empty. I tested the code in a Web Browser and it works. But, when I used it with PhoneGap it did not worked. I tested my code in PhoneGAp with Fecebook WS and it works. This is the combination that I tested:
Test 1
Source: file:///C:/.../index.html (Web Browser)
WebService: http: //localhost/.../getemployees.php
Result: it works
Test 2
Source: PhoneGap
WebService: http: //localhost/.../getemployees.php
Result: it does not works
Test 3
Source: PhoneGap
WebService: https: //graph.facebook.com/OldemarshCr
Result: it works
This is the code:
$.getJSON(url, function (data) {console.log(data);})
.success(function() { console.log("second success"); })
.error(function(jqXHR, textStatus, errorThrown) {
console.log('******* '+"error: " + textStatus+' *******');
});
This is the JSON response:
{"items":[{"id":"10","firstName":"Kathleen","lastName":"Byrne","title":"Sales Representative","picture":"kathleen_byrne.jpg","reportCount":"0"},{"id":"9","firstName":"Gary","lastName":"Donovan","title":"Marketing","picture":"gary_donovan.jpg","reportCount":"0"},{"id":"7","firstName":"Paula","lastName":"Gates","title":"Software Architect","picture":"paula_gates.jpg","reportCount":"0"]}
Thanks,
In ajax call, try and add
$.ajax({
crossDomain: true,
xhrFields: {withCredentials: true}
});
$.ajax did not work... this is the error that I get
{"readyState":0,"responseText":"","status":0,"statusText":"error"}
I tested the code on web browser and it worked.
This is the code:
$.ajax({
crossDomain: true,
xhrFields: {withCredentials: true},
type: 'GET',
url: url,
dataType: 'json',
success: function(response){ console.log(response); }
error: function(error){ console.log('Error: '+error); }
});
Thanks,
Good News!!
I solved the problem. Both methods, getJSON and ajax, are working, the problem was the server.
When I accessed the Web Services from a external server (not localhost) it worked.
Thanks,

AJAX to Sharepoint Server with Phonegap and JQuery Mobile not working

I have the following Problem. In the Phonegap App(for Android) I want to make an AJAX-Call to connect with a Sharepoint Server, with the following Code:
$.ajax({
url:"https://xxx/_vti_bin/lists.asmx",
beforeSend: function( xhr ){
xhr.setRequestHeader(
"SOAPAction",
"http://schemas.microsoft.com/sharepoint/soap/GetListCollection"
);
xhr.setRequestHeader("Content-Type","text/xml; charset=utf-8");
},
dataType:"xml",
contentType: "application/xml; charset=utf-8",
timeout:10000,
type:'POST',
cache: false,
username: "username",
password: "password",
data: soapEnv,
success:function(data) {
// alert data
var serializer = new XMLSerializer();
serialized = serializer.serializeToString(data);
alert(serialized);
},
error:function(XMLHttpRequest,textStatus, errorThrown) {
// alert errors
alert("Error status :"+textStatus);
alert("Error type :"+errorThrown);
alert("Error message :"+XMLHttpRequest.responseXML);
alert("Error statustext :"+XMLHttpRequest.statusText);
alert("Error request status :"+XMLHttpRequest.status);
},
complete: function(jqXHR, textStatus){
alert(textStatus);
}
});
When I try to run it on the Android Emulator the error messages are:
Error status: error
Error type:
Error message: undefined
Error statustext: error
Error request status: 0
However when I try to run it on my Browser (Chrome) with disabled websecurity (because of same origin policy) it works all fine. Phonegap normally shouldn't care about SOP because of the file:/// Protocol. I added the following to 'mobileinit':
$(document).bind("mobileinit", function() {
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
But when I run the same Code in Chrome without websecurity disabled, I get exactly the same errors as in the Android Emulator.
I also tried an AJAX call to wikipedia (with html instead of xml, and GET instead of POST), and that worked without a problem.
Also I think the AJAX to Sharepoint doesn't even get fired (no traffic in Fiddler2, if I managed to configure it the right way)
So I am really stuck with this problem since 2 days now, if anyone knows how to make this ajax call work, it would made me so happy :-)
(soapEnv is the XML envelope, sent to the server)
Well I know that once upon a time jQuery had a bug where it treated a request status of 0 as an error. When running from the file protocol a status of 0 is the same this as a 200 (OK). You may need to update your version of jQuery.
Alternatively to test my theory just do a plain vanilla XHR request to your service to see if it works. Here is my stock example:
http://simonmacdonald.blogspot.com/2011/12/on-third-day-of-phonegapping-getting.html

Categories

Resources