phonegap api call not working - ajax

I have a rails api running on localhost:3000. On my phonegap app i'm trying to login via api , but it give an error("error") and don't reach to the api.
could be some problem with cross domain request, but is said that in the mobile there is no such thing. internet is working .
my ajax:
$.ajax({
url: 'localhost:3000/api/v1/login',
type: "POST",
dataType: "json",
data : {username:"demo",password:"demo"},
success: function(data) {
navigator.notification.alert("ok");
},
error: function(xhr, textStatus, errorThrown) {
console.log("Error status " + status);
}
});
EDIT 2 - solved
You have to to use local ip eg. 192.168.2.2
and add the host in the Cordova.plist in the ExternalHosts key.

It is because you are using "localhost" when you run this code on a device/emulator then "localhost" refers to the device/emulator itself and not the web server that contains your rails API. You'll need to switch "localhost" to the IP address or hostname of your server.

Related

Phonegap Android app web service

I am making Phone-gap app with android SDK using eclipse. In which i called web service for inserting data into database through AJAX call But i think url is not working properly. I am using localhost web service in running with emulator. My ajax call code is as per below.
$.ajax({
type: "POST",
contentType: "application/json",
dataType: "json",
async: false,
cache: false,
url:"http://xxx.xxx.x.xxx:8080/WebService2.asmx/HelloWorld",
data:"{'n':'" + document.getElementById('txtNum1').value + "', 'a':'" + document.getElementById('txtNum2').value + "','p':'" + document.getElementById('txtNum3').value + "'}",
success: function (response)
{
alert("Inserted data successfully");
document.getElementById('txtNum1').value="";
document.getElementById('txtNum2').value="";
document.getElementById('txtNum3').value="";
},
error: function(data) {
alert("Try Again");
}
});
}
If you want to access localhost from emulator then you need to use the IP address 10.0.2.2.
Try the url http://10.0.2.2:8080/WebService2.asmx/HelloWorld for your web-service when accessing it from emulator.
You can read more about Android emulator networking from the resource:-
http://developer.android.com/tools/devices/emulator.html#networkaddresses

Ajax not fetching data but Wget getting the same

I am quering a REST api hosted at amazon servers from my local system. I have shut out the cross domain origin restriction in Google chrome while firing the ajax.
Yet the ajax call fails.
I tried to wget the same URL and also tried file_get_contents() of PHP and received the required JSON object.
Is it possible that at the amazon servers, they have restricted all the ajax calls as in the Google Network tab i can see that the ajax call fails(status).
CODE::
$.ajax({
url :"URL to ec2",
type : 'get',
dataType: "jsonp",
contentType: 'application/json',
success: function (result) {
console.log("OOHKK ::" + result);
},
error : function (xhr, desc, err) {
console.log(xhr);
console.log("Desc: " + desc + "\nErr:" + err);
}
});

phonegap 3.1.0-0.15.0 remote ajax call

I am developing an app in phonegap 3.1.0-0.15.0 and I am having problems with an ajax call to a remote server.
I have done all the default stuff, like added Internet acces, with-listed the domain, but the ajax still get's the 404 header.
My ajax call:
$.ajax({
url: 'http://my-host/bus/app-ajax',
type: 'GET',
dataType: "json",
data: 'type=data_out',
cache: false,
crossDomain: true,
processData: false,
contentType: false,
success: function(ret_data) {
alert(ret_data);
},
error: function(xhr, textStatus, errorThrown) {
alert("Ajax error(ajax_helper.get_categorys) xhr.status: " + xhr.status);
}
});
My code has an IP address in the "url" variable of the ajax call, I just changed it here for security reasons.
If I copy the content of my www folder to a website that is on the same host as my PHP respond script(browsers don't allow cross domain ajax calls) than it works. On the device that I am testing if I open a browser I can access the domain, and get the json response, but the app just gives me 404 all the time.
Any ideas on what I can still try to make this work?
I recreated the project with the verbose "phonegap create" command, added the same code, and it works. Was probably some problems with the old project, because I updated phonegap in it a few times.

Can't access Neo4j REST url on Heroku

I am trying to use an Ajax call from my web app (not running on Heroku) to query a Neo4j installation on Heroku. The Ajax works flawlessly (no errors, expected JSON response) when connecting to my localhost server url at http://localhost:7474/db/data
but fails when connecting to the Heroku server url at http://login:password#dc02bc2c6.hosted.neo4j.org:7322/db/data
When I say "fail," I mean I receive no errors or results (monitoring in Firebug console). However, I can access the Neo4j webadmin console on Heroku (using Chrome or Firefox but not IE) using the following http://login:password#dc02bc2c6.hosted.neo4j.org:7322/webadmin/
The Ajax code is below.
Can anyone suggest what I am doing wrong and how to connect to Heroku?
restServerURL = "http://login:password#dc02bc2c6.hosted.neo4j.org:7322/db/data";
$.ajax({
type:"POST",
url: restServerURL + "/cypher",
crossDomain: true, //recommended by Neo4j (no effect)
dataType: "json",
data: {
query:"START n = node(*) WHERE n.name =~ '(?i)" + request.term + ".*' RETURN n, n.name ORDER BY LOWER(n.name) ASC LIMIT 12", //"like" (=~) search term, case insensitive ((?i)), show first 12 results
},
success: function( data ) {
//process json here
};
}
});
Thanks,
Jeff
try to use:
xhrFields: {
withCredentials: true
},

i am trying to make an ajax request from my phonegap app to servlet running on my laptop,

i am trying to make an ajax request from my phonegap app to servlet running on my laptop,
its hitting server but the ajax request is always going to error part.
If i will do the same request in normal browser working fine.
function cred() {
var username = "hisari";
var password = "kumar";
var ul = "ip" ;
$.ajax({
type: "GET",
url: ul,
dataType: "json",
success: function(msg, textstatus) {
alert("success");
if (msg.id != null)
{
} else if (msg.error_id)
{
//Error
alert('Error logging in:' + msg.error_message);
}
},
error: function(error) {
alert("Error connecting to the servers" + error.message);
}
});
}
please give me a solution ,If possible suggest what can i use instead of servelet for login validation
Are you using 127.0.0.1 or localhost for your IP? If so, you will need to use the actual IP of your machine when testing the app. This is because when you load the app into a device or simulator, 127.0.0.1 will resolve to the actual device or emulator itself, not your machine. (If using Android emulator, you can use 10.0.2.2 to get to your localhost on the machine, see http://developer.android.com/tools/devices/emulator.html#emulatornetworking).
Have you added your host to whitelist in config.xml?
<access origin="http://127.0.0.1*" />
<access origin="http://*.google.com" />

Resources