JQuery ajax call on android - javascript

I have some html and javascript code running on a WIFI module that runs a webserver. At a certain point, a jquery ajax call is done like so:
reqData = JSON.stringify({FileName: L_JSON_FILENAME, CmdArr: [["read", "cmduart", "data"]]});
jQuery.ajax('' + _szFileRequest,
{
data: reqData,
dataType: 'json',
contentType: "application/json",
async: false,
type: 'POST',
success: function (data, textStatus, jqXHR) {
ReturnedColumn = handleData(data, textStatus, jqXHR);
},
error: function (jqXHR, textStatus, errorThrown)
{
ReturnedColumn = "error";
}
});
Now I want to do the same thing on an android app.
I am currently working in Android Studio.
I have read something about a JSONobject and using toString.
But I have also read about using AsyncHttpClient and using RequestParams.
What is the best way to accomplish this call?

I use Retrofit :
https://square.github.io/retrofit/
It's perfect to make HTTP calls, and it has converters, to ease conversions from JSON, XML, and other format to java
Some examples : https://guides.codepath.com/android/Consuming-APIs-with-Retrofit

Since the webpages were on a webserver I only needed to access that webserver. Connecting to it's WIFI and have a webview open the webpage on the webserver did the trick for me.
The JavaScript runs on the webserver and can easily execute the ajax calls.

Related

Office Task Pane app ajax call not working

I'm working on this TaskPane app for office, and I would like to get some data from a php function that resides on our domain.
We're using the same php function in other apps, where I can just call it with some parameters passed to it in the URL, and it returns a simple answer.
Example: https://www.ourdomain.com/phpfunction.php?message=HELLO
The problem is, that I cannot seem to call this function via AJAX, from the TaskPane app, while debugging.
I added our domain to the App Domains in the application manifest, but it didn't help
Here is my AJAX call
function httpGET(theUrl, callback) {
var dataToReturn;
$.ajax({
type: 'GET',
url: theUrl,
cache: false,
async: true,
data: "",
success: function(data, textStatus, result) {
callback(data)
},
error: function (result, textStatus, errorThrown) {
//ALLWAYS GOES HERE
}
});
};
I want to do it in pure javascript, so if it's possible, I don't want to use the custom .NET web service fix for this.
PS: I want to specify, that this exact same call is working perfectly from a mobile app or a browser based app. It's Office who's doing something here...

Google Feed API with jQuery`s AJAX

I'm trying to load a RSS feed using Google's Feed API which gives me a JSON string.
(documentation: https://developers.google.com/feed/).
However, I'm trying to use jQuery's AJAX instead of vanilla JavaScript XHR.
It is not working for some reason, which I can't identify why.
Loading the URL in the browser works, however (get the link in the code below).
I have prepared a jsFiddle: http://jsfiddle.net/gberger/fNwpD/
$.ajax({
url:'http://ajax.googleapis.com/ajax/services/feed/load?hl=ja&output=json-in-script&q=http%3A%2F%2Ffeeds.gawker.com%2Flifehacker%2Ffull&v=1.0&num=3',
success: function(data){
alert(JSON.stringify(data));
},
error: function(error){
alert(this.url);
alert(JSON.stringify(error));
}
});
Simply add dataType: 'jsonp' to your options object. Your code is not working because of the Same-origin policy. JSONP is one way of dealing with this if the server supports it (Feed API does).
$.ajax({
url: 'xy',
success: function () {},
error: function () {},
dataType: 'jsonp'
});
Your working fiddle

Retrieving user_timeline from Twitter in XML

I'm writing a simple app in HTML and Javascript. I'm trying to retrieve my user_timeline via jQuery's .ajax() method. The problem is that I want to retrieve my timeline in XML but I'm keep failing with this simple task. Here's my code:
$.ajax({
type: 'GET',
dataType: 'xml',
url: 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=stepanheller',
success: function(data, textStatus, XMLHttpRequest) {
console.log(data);
},
error: function(req, textStatus, error) {
console.log('error: '+textStatus);
}
});
Weird thing is that when I try exactly the same thing but with JSON instead of XML then the script works.
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=stepanheller',
success: function(data, textStatus, XMLHttpRequest) {
console.log(data);
},
error: function(req, textStatus, error) {
console.log('error: '+textStatus);
}
});
Can you give me some hints what I'm doing wrong with the request? I know that I'm using old version of API but I won't deal with OAuth right now. Thanks.
It is generally impossible to send a Cross-domain ajax request. It's the general rule.
JsonP is the classic way to work around this limitation, and there is no equivalent for Xml according to my knowledge. Depending on your browser compatibility constraints, you can use XHR2 to achieve this.
Otherwise, the only solution is to set up a server proxy.
Client --Ajax--> Your server --HttpRequest--> Twitter

.Next To remove limitation on data send using JSONP

Can we remove the limitation on data send using JSONP. Below is my code. What i am trying to do is to pass 3000 characters(actuallly a image which is converted to base64 data) at a time to service(serviceCall.ashx). As my data is large up to 30,000-40,000 characters i am dividing it in packets(3000 each ) and then sending it. Is there any way i can send this complete data in one go. Reason for switching to JSONP is to avoid the pop up on IE which says 'This page is accessing info that is not.....'. I know as JSONP uses GET method there would obviously a data limitation but is there any way to work around this problem.
$.ajax({
type: "GET",
url: 'http://sys108/restnew1/serviceCall.ashx',
dataType: "jsonp",
contentType: "application/json; charset=utf-8",
async: false,
data: {
datachunk: imgdatachunk,
packetlen: imgdatachunk.length,
imagekey: imageid
},
success: function (data) {},
error: function (jqXHR, textStatus, errorThrown) {
if (window.console)
console.log("Error... " + textStatus + " " + errorThrown);
}
});
No, it's not possible to send a GET request of that length in a more-or-less reliable way: actually, it depends both on how the web server is set up and what client (= browser) is used by someone who works with your application.
So I'd suggest looking for alternative (to JSONP) solutions - like CORS, for example.

Unable to retrieve particular JSON objects using jQuery

I try to retrieve a JSON object with jQuery from a server. Some properties of this object are arrays. When these arrays are not empty, I'm able to process my object. But when I retrieve a JSON like this one :
{"Id":144,"Identifier":"4000011","ContractId":115,"ContractName":"Test4","Meters":[],"Scans":[]}
where "Meters" and "Scans" are empty, jQuery raises an error... I query my server with this code :
$("#test").click(function () {
$.ajax({
type: "GET",
url: "/Gateway/GetDetails/144",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, textStatus, jqXHR) {
...
},
error: function (jqXHR, textStatus, errorThrown) {
...
}
});
In the error handler, I can see my JSON object in the responseText property of the parameter "jqXHR". Did you encounter this problem ?
Thanks in advance !
The JSON you've supplied is valid (as confirmed by the JSON Lint tool); is it possible that the Server you are querying is returning an HTTP Error Status Code, or that an internal error is happening on the server side. You can confirm this by using a debugging proxy like Firebug, Chrome Developer tools.
I answer my own question... First I tested only with Internet Explorer 9; with an other browser, all worked as expected. After I cleared the Internet Explorer cache, the problem disappeared.

Categories