我打電話給一個非常簡單的PHP頁面,但是調用總是返回任何內容,即使PHP很好。也就是說,您可以轉到PHP頁面的URL並看到它迴應「Hello World」,但當它使用JS調用時,它不會返回任何內容。Ajax調用PHP頁面總是返回任何內容,即使PHP頁面迴應什麼東西
下面是HTML頁面的JavaScript:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Title of the document</title>
</head>
<body>
The content of the document......<br />
Enter your email: <input id="email" type="text" />
<input type="button" onclick="setXMLHttpRequest()" value="Go!" />
<script type='text/javascript'/>
var http;
function setXMLHttpRequest()
{
if(window.XMLHttpRequest)
http = new XMLHttpRequest();
else if(window.ActiveXObject)
http = new ActiveXObject("Microsoft.XMLHTTP");
url = "http://www.convolutedconstruct.com /Ajax/checkemail.php?email=" +
document.getElementById('email').value;
http.onreadystatechange = display;
http.open("GET", url, true);
http.send(null);
}
function display()
{
if (http.readyState == 4)
{
infostr = http.responseText;
alert("From the PHP: " + infostr);
}
}
</script></body></html>
這裏是PHP頁面的內容 Click here for the live PHP page
<?php
$email = $_GET['email'];
echo "Hello World!";
?>
爲什麼這回沒事了JS,即使PHP頁面正確地回顯文本?
讓我猜猜,http://en.wikipedia.org/wiki/Same_origin_policy – Adi 2012-08-05 17:40:23
@adnan說,這不是一個有效的路徑AJAX:'convolutedconstruct.com/Ajax/checkemail.php?email ='如果你想跨越原點試試JSONP – Sammaye 2012-08-05 17:41:46
或者只是讓你的路徑:'/Ajax/checkemail.php?email =' – Sammaye 2012-08-05 17:42:13