$.getJSON(twitter_url, function(data){
loadtwit(data);
});
I am querying the twitter url via $.getJSON, for instance:
http://twitter.com/statuses/friends/stevejobs.json?callback=?
Sometimes the browser response is UnAuthorized if the tweets are protected. How can I handle that response to avoid triggering a login form.
success: loadtwit(data);
else: die silently
You can use $.ajax and provide an error handler.
$.ajax({
url: twitter_url,
dataType: 'json',
success: function(data) {
loadtwit(data);
},
error: function(xhr, testStatus, error) {
// handle error
}
});
I think I found something close to a workaround, if not an answer finally.
In my case I wanted to show a user's location, name, photo and some tweets based on their username which I knew, so I was trying to use this:
http://api.twitter.com/1/statuses/user_timeline.json?&screen_name=stevejobs&count=20&callback=?
Which trigger the popup that looks like a phishing scam when querying users with protected tweets.
So you can query a search for tweets based on a user like this:
http://search.twitter.com/search.json?q=from:stevejobs&callback=?
And you can also query a user like this:
http://api.twitter.com/1/users/show/stevejobs.json?&callback=?
The first query will get back tweets and won't ask for passwords because the protected users do not appear in search. Works, but doesn't return location and meta data for the user.
The 2nd query doesn't return tweets, but does return a boolean value for protected.
So if you put it together, you can get a complete search.
I hope someone finds this useful. I've been googling and reading the API all day. After I write the function, I'll come back here and post it.
Related
I'm using the jquery tabledit plug-in to update a database. Works perfectly like in the official examples.
I can succesfuly include a static dropdown with a fixed number of options (defined in custom_table_edit.js).
I'd like to be able to dynamically get those options from a database instead, but I don't know how to customize the code in custom_table_edit.js.
I can code this in php with a loop querying the database and generating a html <select> field. But I don't have knowledge of javascript or if it's even possible in this framework.
This is the custom_table_edit.js file. A dropdown is defined with three colour options. I want this dropdown to be dynamically produced.
// custom_table_edit.js
$('#example2').Tabledit({
url: 'example.php',
eventType: 'dblclick',
editButton: false,
columns: {
identifier: [0, 'id'],
editable: [[1, 'car'], [2, 'color', '{"1": "Red", "2": "Green", "3": "Blue"}']]
}
});
I really haven't tried anything because i'd like to know if it's possible to do in this framework.
Welcome to SO, nucelar.
What you are describing is a HTTP request from the client to server through JavaScript. This is commonly referred as AJAX or Asynchronous JavaScript And XML. This API enables you to manually send requests to the server and there are multiple implementations.
Because you are using jQuery I will recommend you to use the $.ajax function which is included in the jQuery library.
Down here I've made a very basic example of how to send a HTTP request to a server with the GET method to retrieve some data.
$.ajax({
url: 'https://yourdomain.com', // Where to send the request to. Can also be a file.
method: 'GET', // What method of request it uses.
success: function(data) { // When a response is succesfully received.
// Do something with the received data.
console.log(data); // Show what the data looks like in the console.
},
error: function(jqXHR, textStatus, errorThrown) { // When an error occurs while making a request.
console.log(jqXHR, textStatus, errorThrown); // Show the error in the console.
}
});
In your case the url property value might be the URL of a PHP file in which you query the database and return the result, as you mentioned you are able to do.
The response of the AJAX function (which is stored in the data variable in the success method) can be text, as in a string, or even JSON if you want to send structured data.
Beware of the Asynchronous part. This means that the AJAX code does not stop the execution of the rest of your JavaScript code, but simply continues and comes back whenever the HTTP request has been completed.
I hope that this is enough to get you started. Good luck and don't hesitate to ask questions.
I have 2 questions:
1) I'm trying to retrieve a list of posts from Instagram via javascript but I keep getting errors:
$(document).ready(function() {
$.ajax({
url: 'https://www.instagram.com/explore/tags/lion/?__a=1',
dataType: 'jsonp',
type: 'GET',
success: function(data){
console.log(data);
},
error: function(data){
console.log(data);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The error I get:
SyntaxError: missing ; before statement[Learn More]
If I change jsonp to json:
Cross-Origin Request Blocked
How can I overcome this problem?
2)Is it maybe not allowed to get data for my site from the url https://www.instagram.com/explore/tags/lion/?__a=1 ? (maybe they consider it like scraping?)
I have tried to check the official api page but it sounds like only users who have an instagram account and are logged in would be able to see the contents of the feed after retrieving an oauth2 token.
EDIT:
Seems like that to send a request to this url i need jsonp but since I'm not getting the answer in a proper format I'm not sure how can I make this request work. (There isn't already a question like this on stackoverflow about Instagram. The API has recently changed so I'm trying to find a workaround).
According to the Instagram Developers Documentation you should pass a valid access token on the URL using the ?access_token=ACCESS-TOKEN parameter.
https://www.instagram.com/developer/endpoints/tags/
Try removing dataType: 'jsonp' from your AJAX request to get the actual error response from Instagram
I am playing with Google API in javascript. I managed to get a list of my contact with the following code :
$.ajax({
url: 'https://www.google.com/m8/feeds/contacts/default/full?access_token=' + access_token + '&alt=json',
method: 'GET',
error: function(error) {
alert('An error has occured during contact creation.');
},
success: function(data, status){
console.log(data);
}
});
I tried to add a contact by changing GET to POST and adding my contact data in the request body. But as soon as I add a data attribute, or change GET to POST, the server answers me the really annoying "No 'Access-Control-Allow-Origin" error.
Any idea?
I am following this documentation : https://developers.google.com/google-apps/contacts/v3/?csw=1#creating_contacts
Thanks a lot
It is possible to do this from the browser, although not obvious at all.
Based on this SO answer, we learn that there is method called gapi.client.request that can be used for this (instead of jQuery's $.ajax).
Accordingly, for editing we can do:
gapi.client.request({
method : 'PUT',
path:'m8/feeds/contacts/default/full/<contactId>/<editRevisionFromGET>',
body : {"version":"1.0","encoding":"UTF-8","entry": ...},
callback : function(data) {
console.log(data);
}
});
The important parts for editing in here are:
send back the entire entry you got before from a read
use the current ID given at the end of the URL in the entry.link element with relation type edit (or you'll get a HTTP Status 409 - Conflict)
Side note:
Notice that these requests actually are done to https://content.googleapis.com/ ...
From some quick tests, it seems you can do ?all? requests just to that URL instead of google.com, and then CORS issues disappear.
This is my first experience with using JSON so I'm probably doing something really dumb.
I built an app to scrape pages and return a JSON object.
My JSON returned looks like this (using Facebook as an example):
{"urlTitle":"Welcome to Facebook \u2014 Log in, sign up or learn more","urlDescription":" Facebook is a social utility that connects people with friends and others who work, study and live around them. People use Facebook to keep up with friends, upload an unlimited number of photos, post links and videos, and learn more about the people they meet."}
However, in FireBug I get the above error (also see screenshot).
My jQuery code gets the JSON via AJAX and looks simple right now:
$("#submitButton").on("click", function(){
$.ajax({
url: '/miscellaneous/scrape/scrape.cfm',
dataType: 'json',
data: {
strURL: $.param( $("#submitURL").attr("value") )
},
type: 'POST',
success: function(data) {
alert("yes!");
}
});
return false;
});
I never get my success message :( Just the error!
Am I doing something really obviously wrong here?
Thanks,
Michael.
EDIT
Here's my entire JSON as requested:
{"urlTitle":"Welcome to Facebook \u2014 Log in, sign up or learn more","urlImages":{"image_8":"http:\/\/static.ak.fbcdn.net\/rsrc.php\/v2\/yb\/r\/GsNJNwuI-UM.gif","image_6":"http:\/\/photos-g.ak.fbcdn.net\/photos-ak-snc7\/v85005\/226\/255889644513526\/app_104_255889644513526_1061222291.png","image_7":"http:\/\/secure-us.imrworldwide.com\/cgi-bin\/m?ci=ent156564&am=3&ep=1&at=view&rt=banner&st=image&ca=cmp7747&cr=crv72918&pc=plc220331&r=1346702536","image_4":"http:\/\/photos-a.ak.fbcdn.net\/photos-ak-snc7\/v85006\/156\/156324174503268\/app_104_156324174503268_1504955413.png","image_5":"http:\/\/photos-b.ak.fbcdn.net\/photos-ak-snc7\/v85005\/78\/344521295633922\/app_104_344521295633922_1943760717.png","image_3":"http:\/\/photos-c.ak.fbcdn.net\/photos-ak-snc7\/v85005\/14\/298987460188718\/app_104_298987460188718_170436975.png","image_2":"http:\/\/photos-g.ak.fbcdn.net\/photos-ak-snc7\/v85006\/196\/287530028007964\/app_104_287530028007964_853932327.png","image_1":"http:\/\/static.ak.fbcdn.net\/rsrc.php\/v2\/yY\/r\/2LiCtrj0cdC.png"},"urlDescription":" Facebook is a social utility that connects people with friends and others who work, study and live around them. People use Facebook to keep up with friends, upload an unlimited number of photos, post links and videos, and learn more about the people they meet."}
Hi Based on the comments on your question.. looks like you are looking for a way to avoid AJAX requests to do caching. For that you can try something like this..
$.ajax({
url: url,
data: inputs + '&ran=' + Math.rand(),
)};
or
$.ajaxSetup ({
// Disable caching of AJAX responses
cache: false
});
I hope this will help you.
I'm trying to display the follow count of a twitter account, but when I hook into the API using this code:
$.getJSON("https://api.twitter.com/1/users/show.json?screen_name=uswitchTech&include_entities=true", function(data) {
console.log(data);
if (!data.error) {
$("#followers").html(data.followers_count);
}
});
I get a 200 ok report but with Data is null message.
But if I download the json file to my local machine and change the getJSON call accordingly, it works straight away.
Has anyone got any ideas on what could be causing this?
Thanks
Also just to add, if I put the Twitter API url into my browser it displays all the data, which makes it even weirder.
Maybe the problem lies with jsonP, since you are calling a remote server and you must specify you should use jsonP. Have you tried adding callback=? as a parameter
$.getJSON("https://api.twitter.com/1/users/show.json?screen_name=uswitchTech&include_entities=true&callback=?", function(data) {
if (!data.error) {
$("#followers").html(data.followers_count);
}
});
Taken from jQuery docs
JSONP
If the URL includes the string "callback=?" (or similar, as
defined by the server-side API), the request is treated as JSONP
instead. See the discussion of the jsonp data type in $.ajax() for
more details.
$.ajax({
url: 'https://api.twitter.com/1/users/show.json?screen_name=uswitchTech&include_entities=true',
dataType: 'jsonp',
success: function(data){
console.log(data.followers_count);
}
});