Chrome中的AJAX请求responseText为空? [英] AJAX request responseText is empty in Chrome?

查看:584
本文介绍了Chrome中的AJAX请求responseText为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让最简单的AJAX请求成为可能,出于某种原因(出人意料地)在IE中可以使用,但在Chrome中不能使用?



这里是代码:

  var x = new XMLHttpRequest(); 
x.open(GET,style.php,true);
x.send();
alert(x.responseText);

最后一行只是弹出一个空的'alert'窗口。



PHP代码:

 <?php 
header(Content-Type :text / plain);
回显HELLO; ?>

有人建议我在代码之前放置文本/纯文本头,但是不起作用。 Chrome中的JS控制台显示状态为200和800B,因此脚本收到响应,但没有看到它?



非常感谢您提前

解决方案

XMLHttpRequest是一个异步函数。您应该这样做:

  var x = new XMLHttpRequest(); 
x.open(GET,style.php,true);
x.send();
x.onreadystatechange =函数(响应){
alert(response.responseText);
};


I was trying to make the simplest AJAX request possible, which for some reason (unexpectedly) works in IE, but not in Chrome?

Here is the code:

var x = new XMLHttpRequest();
x.open("GET","style.php",true);
x.send();
alert(x.responseText); 

The last row merely makes an empty 'alert' window pop up.

The PHP code:

    <?php
     header("Content-Type: text/plain");
     echo "HELLO"; ?>

Someone suggested that I put text/plain header before the code, didn't work. The JS console in Chrome shows status as 200, and 800B as received, so the script receives the response, but doesn't see it?

Thank you very much in advance

解决方案

XMLHttpRequest is an asynchronous function.

You should do this :

var x = new XMLHttpRequest();
x.open("GET","style.php",true);
x.send();
x.onreadystatechange = function(response) {
 alert (response.responseText);
};

这篇关于Chrome中的AJAX请求responseText为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆