Mi llamada AJAX no devuelve nada, pero no está fallando, ya sea
-
21-09-2019 - |
Pregunta
Tengo este código HTML:
<input type="text" id="text"/>
<input type="button" id="submit" value="Submit" />
<div id="twitter_update_list">
</div>
y esto 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);
}
}
}
Busco al quemador, y la veo verything es enviado correctamente pero no recibe ninguna respuesta en absoluto. Oh, y estoy usando javascript prima porque quiero practicarlo. =)
Solución
Usted está tratando de hacer XMLHttpRequest entre dominios. No funciona aunque el servicio puede ser apoyando JSONP. Para hacer una solicitud JSOP, que sea necesario incorporar una etiqueta <script>
con los controladores de eventos correctos o utilizar un marco como jQuery.
Otros consejos
Si utiliza jQuery sencilla que podría hacer esto
$.load("http://twitter.com/statuses/user_timeline/"+username,
function(data)
{
alert(data);
}
);
que imprimirá el usertimeline dada para el nombre de usuario
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow