Retrieving user_timeline from Twitter in XML - javascript

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

Related

JQuery ajax call on android

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.

How to access Recurly API with AJAX?

I'm using a jquery ajax call to a recurly API endpoint, but I get cross-origin errors. From my understanding, this is because Recurly only returns results as XML... when I use JSONP to get around cross-origin errors, I get an error because it receives the XML data but expects JSONP. Pretty obvious. But I'm trying to understand how exactly can one use this API at all via AJAX calls. I've been successfully able to access the API with PHP, but unfortunately, for this project, I can't use any client-side code.
Even if I find some sort of middle-code solution to get the XML and convert it to JSON for my side to accept, I need to utilize the API for POST requests (creating accounts, subscriptions, etc.) so I would like to understand how to utilize the API properly.
Here is an example of my code:
$.ajax({
url: "http://[DOMAIN].recurly.com/v2/accounts",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + window.btoa("[API KEY]"));
},
crossDomain: true,
type: "GET",
accepts: "application/xml",
dataType: "application/xml; charset=utf-8",
success: function (data) {
console.log("SUCCESS:", data);
},
error: function(e){
console.log("ERROR:", e);
}});
Anyone with Recurly API experience have any tips/advice?
From https://docs.recurly.com/api/recurlyjs/jsonp_endpoints
$.ajax({
dataType: 'jsonp',
url: 'https://{subdomain}.recurly.com/jsonp/{subdomain}/plans/{plan_code}',
data: {
currency: 'USD',
},
success: function (data) {
// do stuff
},
}
You should not use the V2 API from the browser. Doing so risks exposing your private API key. If someone has your API key they can make calls charging customers, modifying subscriptions, causing all sorts of problems.
Look at the JSONP endpoints that Byaxy linked to.

JSON parseError - get json as string

I'm trying to get json data from a service, but getting parseError when I use $.ajax with datatype 'JSONP':
$.ajax({
url: url,
dataType: 'JSONP'
})
.error(function(XMLHttpRequest, textStatus, errorThrown) { ... })
.done(function(data) { ... });
If i try it with other datatype than 'JSONP' it returns 404 error.
How can i get just a string instead of parsing json, i believe there is some linebreaks in json that cause parse errors.
Here is the fiddle http://jsfiddle.net/FSEZQ/3/
That's JSON, not JSONP.
For example, this is JSON:
{"key": "value"}
This is JSONP:
callback({"key": "value"})
If the service doesn't provide JSONP, the browser prevents you from getting it (same origin security restrictions).
The way people get around same origin restrictions consist of some server utilization. You can either right code that does this in PHP, or use a service such as AnyOrigin .
Here's an AnyOrigin example.
$.getJSON('http://anyorigin.com/get?url=metservice.com/publicData/tides2MonthAuckland&callback=?', function (data) {
$('#result1').html(JSON.stringify(data.contents));
}).fail(function (XMLHttpRequest, textStatus, errorThrown) {
$("#result2").html(textStatus);
});
... and an accompanying fiddle.
Here's an example that shows how this data can be used.

Detect HTTP 301 status code from JavaScript

I need to detect client side if a requested file (with XMLHttpRequest) had a 301 response. The reason of doing this is because I need to request other files related to the file where user has been redirected and not related to the first one requested.
Any way to detect this response status code using JavaScript or JQuery?
Thanks.
jQuery ajax callback function gives out a lot of info
$.ajax({
url: "test.php",
type: "GET",
data: {},
dataType:"json",
success: function(resp, textStatus, xhr) {
//status of request
console.log(xhr.status);
},
complete: function(xhr, textStatus) {
console.log(xhr.status);
}
});
You can use $.ajax with jquery
It has everything you need here : http://api.jquery.com/jQuery.ajax/
If you look at "statusCode" explaination, you will see you can make something for every code

.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.

Categories