Javascript 从twitter中提取推文时出错 啁啾 window.onload=函数(){ //设置表单按钮的单击处理程序 var按钮=document.getElementById(“提交”); button.onclick=getTweets; } //当您单击“获取推文”时,我们称之为此函数 函数getTweets(){ //设置新的XHR请求 var xhr=new XMLHttpRequest(); //我们调用search.php并传入一个查询字符串 var url=“search.php?query=”; var query=document.getElementById(“查询”).value; 如果(!查询){ query=“html5”; } //我们对查询进行编码以正确处理任何特殊字符 url+=encodeURIComponent(查询); //这是当XHR请求时调用的函数 //处理search.php脚本,并返回响应 xhr.onload=函数(){ //如果一切正常,则发送响应数据 //指向displayTweets()函数 如果(xhr.status==200){ 显示tweets(xhr.responseText); }否则{ var errorDiv=document.getElementById(“错误”); errorDiv.innerHTML=“获取推文时出错:”+xhr.status; } }; //提出请求! xhr.open(“GET”,url); xhr.send(空); } 功能显示tweets(tweets){ //tweets是一个很长的字符串,所以我们需要解析它 //首先转换为JSON tweets=JSON.parse(tweets); var ul=文件查询选择器(“ul”); //从列表中清除现有推文 while(ul.hasChildNodes()){ ul.removeChild(ul.lastChild); } //添加新推文 对于(var i=0;i
在上面的代码中,当我在文本框中输入一些文本并单击“获取Tweets”按钮时,会出现一个错误,即Javascript 从twitter中提取推文时出错 啁啾 window.onload=函数(){ //设置表单按钮的单击处理程序 var按钮=document.getElementById(“提交”); button.onclick=getTweets; } //当您单击“获取推文”时,我们称之为此函数 函数getTweets(){ //设置新的XHR请求 var xhr=new XMLHttpRequest(); //我们调用search.php并传入一个查询字符串 var url=“search.php?query=”; var query=document.getElementById(“查询”).value; 如果(!查询){ query=“html5”; } //我们对查询进行编码以正确处理任何特殊字符 url+=encodeURIComponent(查询); //这是当XHR请求时调用的函数 //处理search.php脚本,并返回响应 xhr.onload=函数(){ //如果一切正常,则发送响应数据 //指向displayTweets()函数 如果(xhr.status==200){ 显示tweets(xhr.responseText); }否则{ var errorDiv=document.getElementById(“错误”); errorDiv.innerHTML=“获取推文时出错:”+xhr.status; } }; //提出请求! xhr.open(“GET”,url); xhr.send(空); } 功能显示tweets(tweets){ //tweets是一个很长的字符串,所以我们需要解析它 //首先转换为JSON tweets=JSON.parse(tweets); var ul=文件查询选择器(“ul”); //从列表中清除现有推文 while(ul.hasChildNodes()){ ul.removeChild(ul.lastChild); } //添加新推文 对于(var i=0;i,javascript,php,html,twitter,Javascript,Php,Html,Twitter,在上面的代码中,当我在文本框中输入一些文本并单击“获取Tweets”按钮时,会出现一个错误,即获取Tweets时出错:0。当search.php独立执行而不嵌入html和javascript时,会给出准确和必需的结果。请检查html和js代码,并建议对代码进行任何更改???问题似乎是跨域Ajax问题。当您独立执行search.php时,就不存在X域问题。但是,当您在某些html中嵌入代码时,请检查html是否属于同一域。另外,如果您试图从以下文件运行html文件:///它将不起作用。有一种简单的
获取Tweets时出错:0
。当search.php独立执行而不嵌入html和javascript时,会给出准确和必需的结果。请检查html和js代码,并建议对代码进行任何更改???问题似乎是跨域Ajax问题。当您独立执行search.php时,就不存在X域问题。但是,当您在某些html中嵌入代码时,请检查html是否属于同一域。另外,如果您试图从以下文件运行html文件:///它将不起作用。有一种简单的方法可以自己调试:在Chrome或Firefox中打开开发者工具(F12),然后打开“网络”选项卡。在那里,您将确切地看到发出了什么HTTP请求以及响应是什么。可能缺少某个ID或类似的内容,您从服务器收到一个500错误。调试后,它在search.php中将错误显示为“找不到元素”。我应该如何解决它?将以下行修改为if(xhr.status==200)到if(xhr.readyState==4&&xhr.status==200)和xhr.open(“GET”,url)到xhr.open(“GET”,url,true)尽管按照您的建议更新了代码,但在第40行给出的错误是“跨源请求仅支持HTTP”。我在本地主机上尝试了该代码,即我使用wamp服务器,然后给出的错误消息是“获取推文时出错:200”请专家们帮助我,我不知道如何检查跨域和解决这一问题。ypu能详细说明吗?
<!doctype html>
<html>
<head>
<title>Twitter</title>
<meta charset="utf-8">
<script>
window.onload = function () {
// set up the click handler for the form button
var button = document.getElementById("submit");
button.onclick = getTweets;
}
// when you click "Get Tweets" we call this function
function getTweets() {
// set up a new XHR request
var xhr = new XMLHttpRequest();
// we're calling search.php and passing in a query string
var url = "search.php?query=";
var query = document.getElementById("query").value;
if (!query) {
query = "html5";
}
// we encode the query to handle any special characters properly
url += encodeURIComponent(query);
// this is the function that is called when the XHR request
// to our search.php script is handled, and a response sent back
xhr.onload = function () {
// if everything went okay, then send the response data
// to the displayTweets() function
if (xhr.status == 200) {
displayTweets(xhr.responseText);
} else {
var errorDiv = document.getElementById("error");
errorDiv.innerHTML = "Error getting tweets: " + xhr.status;
}
};
// make the request!
xhr.open("GET", url);
xhr.send(null);
}
function displayTweets(tweets) {
// tweets is a big long string, so we need to parse it
// into JSON first
tweets = JSON.parse(tweets);
var ul = document.querySelector("ul");
// clear existing tweets from list
while (ul.hasChildNodes()) {
ul.removeChild(ul.lastChild);
}
// add new tweets
for (var i = 0; i < tweets.length; i++) {
var li = document.createElement("li");
li.innerHTML = tweets[i].tweet;
ul.appendChild(li);
}
}
</script>
</head>
<body>
<form>
Query:
<input type="text" id="query">
<input type="button" id="submit" value="Get Tweets">
</form>
<div id="error"></div>
<ul></ul>
</body>
</html>