admin管理员组文章数量:1392007
I'm trying to make my first ajax example working on my MAMP. my ajax.html looks like:
<html>
<head>
<script src='ajax.js'></script>
</head>
<body onload = 'ajax()'>
<div id='test'></div>
</body>
</html>
my ajax.js looks like:
function ajax() { >>var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","http://localhost:8888/ajax.php",true); xmlhttp.send(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("test").innerHTML=xmlhttp.responseText; } } }
my ajax.php looks like:
echo 'hello world';
I detected response header from firebug:
Response Headers
Connection Keep-Alive
Content-Length 11
Content-Type text/html
Date Mon, 05 Nov 2012 18:57:46 GMT
Keep-Alive timeout=5, max=99
Server Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/0.9.8r DAV/2 PHP/5.4.4
X-Pad avoid browser bug
X-Powered-By PHP/5.4.4
but nothing in response text and nothing changed in my html.
Anyone could help me out please?
Thanks!
I'm trying to make my first ajax example working on my MAMP. my ajax.html looks like:
<html>
<head>
<script src='ajax.js'></script>
</head>
<body onload = 'ajax()'>
<div id='test'></div>
</body>
</html>
my ajax.js looks like:
function ajax() { >>var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","http://localhost:8888/ajax.php",true); xmlhttp.send(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("test").innerHTML=xmlhttp.responseText; } } }
my ajax.php looks like:
echo 'hello world';
I detected response header from firebug:
Response Headers
Connection Keep-Alive
Content-Length 11
Content-Type text/html
Date Mon, 05 Nov 2012 18:57:46 GMT
Keep-Alive timeout=5, max=99
Server Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/0.9.8r DAV/2 PHP/5.4.4
X-Pad avoid browser bug
X-Powered-By PHP/5.4.4
but nothing in response text and nothing changed in my html.
Anyone could help me out please?
Thanks!
Share Improve this question edited Nov 5, 2012 at 20:36 Stephen P 14.8k2 gold badges48 silver badges70 bronze badges asked Nov 5, 2012 at 19:15 Mengyu ZHangMengyu ZHang 1231 silver badge10 bronze badges1 Answer
Reset to default 12Your problem is that you're trying to do a cross domain request. The browser prevents it due to same origin policy.
The standard solution is to set CORS headers in your PHP to allow those requests.
For example :
<?php
header("Access-Control-Allow-Origin: *");
?>
本文标签: javascriptAjax call to PHP is returning nothingStack Overflow
版权声明:本文标题:javascript - Ajax call to PHP is returning nothing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744761423a2623777.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论