Ajax conection fails - ajax

I made a mobile app using Phonegap. I use Ajax to get the data from the server. The server has a Laravel aplication that give the data in Json. This is the code:
jQuery(function($) {
$(document).ready(function() {
$(".loader").show();
$.ajax({
url: 'http://miIp/public/menu',
dataType: 'json',
success:function(data){
$("#conectionError").hide();
if(data == 1){
$('#liveStreaming').show();
$("#live_choose").show();
$(".btn_menu").hide();
}
if(data == 0){
$('#liveStreaming').hide();
$("#live_choose").hide();
$(".btn_menu").show();
$('#liveStreaming').hide();
}
$(".loader").fadeOut("slow");
},
error:function(){
$(".loader").hide();
var divHeight = $("#conectionError").height();
var imgHeight = $("#conectionErrorImg").height();
var imgMargin = (divHeight - imgHeight)/2;
$("#conectionErrorImg").css("margin-top", imgMargin);
$("#conectionError").show();
},
async: true
});
});
});
I tested the app locally (I had the laravel aplication in Xampp) and works ok. I even compile the app with phonegap build, and I tested it in my phone, and works ok.
Today I uploaded the Laravel aplication to a cloud server on WIROO, but the mobile app doesn't get data. I put the links on chrome and I can see the data in Json, but ajax fails over and over again.
I supose tha may be is a permission issue (I am using Vesta Panel), but I don't understand anything of this.
¿Can somebody help me?

Related

Cordova Blackberry update to 10.3.1 - AJAX POST JSON returns Data null

I am developing an app for BlackBerry 10 using cordova.
Since I updated my BlackBerry OS from 10.2.1 to 10.3.1 I have the following problem: When I do a Ajax Post with JSON I don't get a data result, but step into the success function.
My code looks like this:
window.jQuery.ajax({
url: "http://myurl.com/systemservice.asmx/ServiceName",
data: "{'DataName':'DataValue'}",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
//Do something else with data.d
}});
The alert returns "null".
Testing the App in a web Browser (also build it as Browser App with cordova) works fine. Even with the old Version of BlackBerry OS it worked fine. Any ideas?
I have a couple of my Cordova apps running on 10.3.1 and they seem to still work fine... though I'm using Angular's $http to make the calls.
I would start by trying to build the app with WebInspector enabled (use the WebWorks SDK GUI, or it's the -d flag while building, I think) to check the console for errors. Or use this tidbit to echo console output into alert messages:
var _log = console.log,
_warn = console.warn,
_error = console.error;
console.log = function() {
alert(JSON.stringify(arguments));
return _log.apply(console, arguments);
};
console.warn = function() {
alert(JSON.stringify(arguments));
return _warn.apply(console, arguments);
};
console.error = function() {
alert(JSON.stringify(arguments));
return _error.apply(console, arguments);
};
Good luck!

Sencha Ajax call not working

I’m trying to access a web service with Sencha touch. With an JSONP call from the browser, all works fine. When we switch the application to a physical device, and change the JSONP request (no longer needed on an real device) to an ordinary AJAX call, nothing happens.....
What is the right way, to fix this? We're using PHONEGAP, I think something probably is wrong with the container, because the call itself looks like it should (-> And is working in the browser, where PHONEGAP is not needed and therefore not included.)
Here is my code:
Ajax-Call for the Mobile (not working)
// 1. When we use this on the DEVICE, it DOESEN'T work.
// It seems, like the call isnt fired at all
// JSONP we don't need here......
alert("Ajax request is send");
Ext.Ajax.request ({
url: Adress_To_Webserver,
method: 'GET',
timeout: 1000,
success: function(response, opts) {
alert("sucess");
//alert(Ext.decode(response.responseText));
//var obj = Ext.decode(response.responseText);
console.dir(obj);
},
failure: function(response, opts) {
alert("error");
}
});
JSONP-Call for the Browser (working)
// 2. If we do the same Call (JSONP) on the BROWSER, it WORKS.
alert("Ajax request is send");
Ext.data.JsonP.request ({
url: Adress_To_Webserver,
timeout: 1000,
callbackKey: "myjson",
params: {
callback: function (jsonData) {
}},
success: function(response, opts) {
alert("sucess");
alert(Ext.decode(response.responseText));
var obj = Ext.decode(response.responseText);
console.dir(obj);
},
failure: function(response, opts) {
alert("error");
}
});
It would be great, if someone have a hint/solution for this problem - like i mentioned, i'm using sencha architect to build a android app. The Phonegap folder with the .apk is created automaticaly, all permissions in the config.xml are set. Phonegap plugins have been also installed. Probably, some more dependencies are missing or wrong - what will be a good place for looking?
Thanks in advance for helping,
Daniel
It seems to me that you don't set up the <access> property in the config.xml file to origin="*" or whatever domain you need. This property specifies all exteranal domains which phonegap allows your web app to access.
So, your config.xml file should contain the following line:
<access origin="*" />
Instead of '*' your can put your resource from which you retrieve you data.
There is the explicit description how to use it white list (phonegap)

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" />

302 in JQuery Mobile App

I have a JQuery Mobile app. This app is currently running on localhost. I am relying on some services hosted on some of my backend servers. When I run a query, I noticed in fiddler that I am getting a 302. This 302 is causing my service call to fail.
If I copy-and-paste the URL of the service in my browser window, everything works fine. Here is my JQuery code
$.ajax({
url: GetServiceUrl(),
type: "GET",
data: vm,
contentType: "application/json",
success: service_Succeeded,
error: service_Failed
});
function service_Succeeded(result) {
alert("received!");
}
function service_Failed() {
alert("oops");
}
function GetServiceUrl() {
var url = "http://www.mydomain.com/services/endpoint";
return url;
}
Why am I getting a 302 from my app?
Thank you
your service is sending a redirect request. Maybe you require a login?

ASP.NET MVC3 & jquery.getJSON not making request

I am working on a MVC 3 site and everything was working until I util I upgraded to jquery1.7.1 using nuget. Now my $.getJSON calls are not making the request to the server. Fiddler does not show a request being made back to the server and code before and after the $.getJSON is executing. Any ideas would be appreciated.
Here is a sample client script:
var srvBaseUrl = "http://localhost:15627/Home/";
$.getJSON(srvBaseUrl + "GetInfo", { 'id': 1 }, function (allData) {
alert(allData);
});
and the corresponding server code:
[HttpGet]
public ActionResult GetInfo(int id)
{
return Json(id, JsonRequestBehavior.AllowGet);
}
I tested that the server code works building up the request in Fiddler.
I had a similar problem, it turned out that JQuery was caching the results. This would explain your comments above that it started working after you made a change.
If you have a look at http://api.jquery.com/jQuery.getJSON/, you will see that $.getJSON is just shorthand for
$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});
If you use the $.ajax instead of the shorthand $.getJSON you can disable caching using the cache parameter, meaning you should be able to replace your code with:
$.ajax({
url: srvBaseUrl + "GetInfo",
dataType: 'json',
data: { 'id': 1 },
success: function (allData) { alert(allData); },
cache: false
});
Nuget is not going to update your Views/Shared/_Layout.cshtml file with the updated reference to the latest version of jQuery installed. You will need to update this yourself:
<script src="#Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"> </script>
EDIT
Assuming that is all in order like you said, make sure your getJSON call is inside a document ready function as it work as-is for me. I would however change how you are building the Url to use the helper method instead:
<script type="text/javascript">
(function ($) {
$.getJSON('#Url.Action("GetInfo")', { 'id': 1 }, function (allData) {
alert(allData);
});
})(jQuery);
</script>
This tested out fine in a stock ASP.NET MVC3 Internet Application template project using your controller action as-is.

Resources