我想向twitter api提出请求.这是文档(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
Run Code Online (Sandbox Code Playgroud)
文档上没有示例请求.对此网址的请求如何包含数据响应警报?
WoL*_*lus 17
看看这有用,我为你做了一个例子:
基本上HTML代码包含2个输入.一个用于按钮,一个用于查询字符串.
<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>
Run Code Online (Sandbox Code Playgroud)
按下搜索按钮后,您将向twitter发送请求,询问包含查询字符串的5个结果(rpp).
这是此页面的javascript:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
诀窍是jQuery.param()函数,你将传递params用于搜索/请求
看到它在这里运行: