Twitter API的示例JSON HTTP请求? [英] Example JSON HTTP request for twitter api?
问题描述
我想向twitter api发出请求.这是文档(https://dev.twitter.com/docs/api/1/get/search)中提供的示例:
I want to make a request to the twitter api. This is the example provided in the documentation (https://dev.twitter.com/docs/api/1/get/search):
GET:
http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed
文档上没有示例请求.该URL的请求将如何包括带有数据响应的警报?
There is no example request on the documentation. How would the request for this url be including an alert with data response?
推荐答案
看是否有帮助,我为您提供了一个示例:
Look if this helps, I made an example for you:
基本上,HTML代码包含2个输入.一个用于按钮,另一个用于查询字符串.
Basically the HTML code contains 2 inputs. one for the button and one for the query string.
<html>
<head>
<title>example</title>
</head>
<body>
<div style="padding: 20px;">
<input id="query" type="text" value="blue angels" />
<input id="submit" type="button" value="Search" />
</div>
<div id="tweets" style="padding: 20px;">
Tweets will go here.
</div>
</body>
</html>
按下搜索按钮后,您将向Twitter发送请求,要求包含查询字符串的5个结果(rpp).
After pressing the search button, you'll send a request to twitter asking for 5 results (rpp) containing the query string.
这是此页面的javascript:
Here's the javascript for this page:
function searchTwitter(query) {
$.ajax({
url: 'http://search.twitter.com/search.json?' + jQuery.param(query),
dataType: 'jsonp',
success: function(data) {
var tweets = $('#tweets');
tweets.html('');
for (res in data['results']) {
tweets.append('<div>' + data['results'][res]['from_user'] + ' wrote: <p>' + data['results'][res]['text'] + '</p></div><br />');
}
}
});
}
$(document).ready(function() {
$('#submit').click(function() {
var params = {
q: $('#query').val(),
rpp: 5
};
// alert(jQuery.param(params));
searchTwitter(params);
});
});
诀窍是jQuery.param()函数,您将为搜索/请求传递参数
The trick is the jQuery.param() function that you'll pass the params for the search/request
看到它在这里运行:
这篇关于Twitter API的示例JSON HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!