[英]My AJAX call is not returning anything but isn't failing either
I have this html: 我有这个HTML:
<input type="text" id="text"/>
<input type="button" id="submit" value="Submit" />
<div id="twitter_update_list">
</div>
and this javascript: 和这个JavaScript:
var xmlHttp;
document.body.onclick = function(){
var username = document.getElementById('text').value;
selectUser(username);
}
function selectUser(username){
var url = "http://twitter.com/statuses/user_timeline/" + username + ".json?callback=twitterCallback2&count=100";
try{// Opera 8.0+, Firefox, Safari
xmlHttp = new XMLHttpRequest();
}catch (e){// IE
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
xmlHttp.onreadystatechange = processRequest;
xmlHttp.open( "GET", url, true );
xmlHttp.send( null );
}
function processRequest(){
if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)) {
if ( xmlHttp.responseText == "Not found" ) {
document.getElementById('twitter_update_list').innerHtml = "Not found";
}else if(xmlHttp.responseText == " "){
document.getElementById('twitter_update_list').value = "Empty";
}else{
// No parsing necessary with JSON!
document.getElementById('twitter_update_list').value = xmlHttp.responseText;
console.log(xmlHttp.responseText);
}
}
}
I am looking at firebug and I see verything gets sent correctly but I don't get any response whatsoever. 我正在看萤火虫,看到一切都正确发送了,但是却没有任何回应。 Oh, and I'm using raw javascript because I want to practice it.
哦,我要使用原始javascript,因为我想练习它。 =)
=)
You are trying to do cross-domain XMLHttpRequest. 您正在尝试执行跨域XMLHttpRequest。 It doesn't work although the service may be supporting JSONP.
尽管该服务可能支持JSONP,但它不起作用。 To do a JSOP request, you either need to inject a
<script>
tag with the correct event handlers or use a framework such as jQuery. 要执行JSOP请求,您需要使用正确的事件处理程序注入
<script>
标记,或使用jQuery之类的框架。
If you use jquery you could simple do this 如果您使用jQuery,则可以简单地执行此操作
$.load("http://twitter.com/statuses/user_timeline/"+username,
function(data)
{
alert(data);
}
);
which will print the usertimeline for the given username 这将打印给定用户名的usertimeline
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.