获得使用基本的JSON Twitter的API [英] getting twitter api using basic json
本文介绍了获得使用基本的JSON Twitter的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我与jQuery非常新,我试图让使用JSON Twitter的API寿我设法用PHP做它,我写了这个简单的一块code,但它似乎并没有工作。
(函数()
{
$(文件)。就绪(函数()
{
$.getJSON(\"https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true\",function(data)
{
VAR ragzor = data.name;
$(。ragzor)文本(ragzor)。
的console.log(ragzor);
});
返回false;
});
});
解决方案
的jQuery(函数($){//短以$(文件)。就绪(函数(){和您确保$指的是jQuery的。
$阿贾克斯({//所有的jQuery Ajax调用去扔在这里。$ .getJSON只是为$就重写功能
网址:https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true
数据类型:JSONP,// dataType的设置为JSONP(重要)
成功:函数(RESP){
的console.log(RESP); //这里RESP重新presents从Ajax调用有限公司Reseved的数据。
}
});
});
jQuery的源$ C $ C:
$。=的getJSON功能(网址,数据,回调){
返回jQuery.get(网址,数据,回调,JSON);
}
您重定向到 $。获得
jQuery.each([获取,后],功能(我,法){
jQuery的[法] =功能(网址,数据,回调,类型){
如果忽略了数据参数//移位参数
如果(jQuery.isFunction(数据)){
type =类型||回电话;
回调=数据;
数据=不确定;
} 返回jQuery.ajax({
类型:方法,
网址:网址,
数据:数据,
成功:回调,
数据类型:类型
});
};
});
屁股你可以看到这个返回 $的初始化版本。AJAX
所以你的电话重写为:
$。阿贾克斯({
网址:......
数据类型:JSON//< - 注JSON不是JSONP,
成功:函数(){
// ...
}
});
I am fairly new with jQuery and I was trying to get the twitter api using JSON tho I managed to do it with php I wrote this simple piece of code but it does not appear to work
(function()
{
$(document).ready(function()
{
$.getJSON("https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true",function(data)
{
var ragzor = data.name;
$(".ragzor").text(ragzor);
console.log(ragzor);
});
return false;
});
});
解决方案
jQuery(function($) { // Shorter for $(document).ready(function() {, and you make sure that $ refers to jQuery.
$.ajax({ // All jQuery ajax calls go throw here. $.getJSON is just a rewrite function for $.ajax
url: "https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true",
dataType: "jsonp", // dataType set to jsonp (important)
success: function( resp ) {
console.log( resp ); // Here resp represents the data reseved from the ajax call.
}
});
});
jQuery source code:
$.getJSON = function( url, data, callback ) {
return jQuery.get( url, data, callback, "json" );
}
Redirect you to $.get
jQuery.each( [ "get", "post" ], function( i, method ) {
jQuery[ method ] = function( url, data, callback, type ) {
// shift arguments if data argument was omitted
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
data = undefined;
}
return jQuery.ajax({
type: method,
url: url,
data: data,
success: callback,
dataType: type
});
};
});
Ass you can see this returns a initialized version of $.ajax
So your call rewrites to:
$.ajax({
url: "...",
dataType: "json" // <- Note json not jsonp,
success: function() {
// ...
}
});
这篇关于获得使用基本的JSON Twitter的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文