我试图创建一个jquery auto compelete。当我测试我是否通过ajax连接到php文件时,我收到以下错误。发生了什么事?
错误:XMLHttpRequest无法加载file:/// C:/Users/Asus/Desktop/UI_lab/lab4/getflights.php?flight = n。收到无效回复。起源' null'因此不允许访问。
更新: 好吧,我在学校服务器上有所有必要的文件并通过vpn连接到它,我收到此错误告诉我该变量未定义!错误:ReferenceError:$未定义Lab44.html:19
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script src="js/jquery-1.11.1.min.js"></script>
<script src = "js/jquery-ui-1.10.4.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.10.4.min.css" />
<style type=text/css>
#form1 {
background-color: #ff9900;
width: 200px;
border: #000 double medium;
padding: 10px;
}
</style>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#flight").keyup(function () {
var flight = $("#flight").val();
$.ajax({
url: "getflights.php",
data: "flight=" + flight,
success: function(msg) {
alert(msg);
}
});
});
//TODO1: Use autocomplete for input#flight
//With AJAX get the list of flights (getflights.php)
//Fill the list with the list of flights in n response (Make a javascript array of those)
//See: autocomplete and options 'source' (Use type Function: in function you get the list with AJAX)
//TODO2: For inputs date1 and date2 use Datepicker
//2 months seen and other month's days are shown
//For date2 set the the date of selected date1 (From and To dates)
//TODO3: Set event handler for submit button
//Use slide and puff animations
//Dialog is modal
//Get the flight and dates from Form
//Add one button 'close'
});
</script>
</head>
<body>
<form id="form1">
<h2> Flight Reservation </h1>
<h3>Enter :</h3>
<input id="flight" />
<h3>Click to select a dates:</h3>
<h5> From </h5>
<input id=date1 />
<h5> To </h5>
<input id=date2 />
<div id="dialog" title="Window title">
<p> </p>
</div>
<input type="button" name="submit" value="Submit" id="submitbutton" />
<input type="reset" name="reset" value="Clear" id="resetbutton" />
</form>
</body>
</html>
答案 0 :(得分:-1)
您似乎试图在没有运行实际网络服务器的情况下使用PHP(即您使用file://协议访问网站)。为了使用PHP,您需要一个运行PHP的Web服务器(例如apache),它将在浏览器请求时对您的.php文件进行预处理。为了便于使用Web服务器安装和设置PHP,您可能需要尝试XAMPP。
此外,即使您的文件不需要PHP,Chrome也不允许您对file://
文件使用AJAX。
简而言之,要使用AJAX和PHP进行Web开发,您需要让自己运行一个Web服务器。