我用POST
编写了一个jquery ajax,但它没有用。经过几个小时的搜索,我发现通过更改为GET
可能会解决问题。幸运的是它有效,但仍然存在POST
无效的问题。这是我的代码:
<html>
<head>
<title>Hello this is my title</title>
<script type = "text/javascript"
src = "jquery.js"></script>
<script>
$(document).ready(function() {
$("#driver").click(function(event){
debugger;
$.ajax({
type: "POST",
url: "login.php",
data: { name:"Zara" },
dataType: "text",
success: function(data) {
$('#stage').html(data);},
error: function(j,t,e) {
$('#stage').text('An error occurred= ' + j + " *** " + t + " *** " + e);
}
});
});
});
</script>
</head>
<body>
<p>Click on the button to load login.php file −</p>
<div id = "stage" style = "background-color:wheat;">
STAGE
</div>
<input type = "button" id = "driver" value = "Load Data" />
</body>
</html>
这是PHP代码:
if( empty($_REQUEST['name'] )) {
$_REQUEST['name'] = "Error";
}
$name = $_REQUEST['name'];
echo "Welcome ". $name;
运行此代码后,它会显示&#34; not found&#34;。我已经搜索了整个网站,jquery官方网站以及有关stackoverflow的所有类似问题,但它们都没有为我工作!
======
这是请求标题:
Host: localhost:63342
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0
Accept: text/plain, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:63342/untitled2/index.php?_ijt=qgukfccd8om4ruh3e8fv7v8rds
Content-Length: 9
Cookie: Phpstorm-ed364c84=959e64e3-228a-4a78-904d-cc31d3f1a3e0
Connection: keep-alive
这是请求正文:
name=Zara
这是回复:
<!doctype html>
<title>404 Not Found</title>
<h1 style="text-align: center">404 Not Found</h1>
<hr/><p style="text-align: center">
PhpStorm 2016.2.1</p>
答案 0 :(得分:1)
AJAX请求需要网络服务器。它不会只在你的本地笔记本电脑上运行,这解释了为什么POST没有做任何事情。
答案 1 :(得分:0)
试一试后,
if(!isset($_POST['name'])) {
$_POST['name'] = "Error";
}
$name = $_POST['name'];
echo "Welcome ". $name;
答案 2 :(得分:0)
我在XAMP,htdocs文件夹中复制了我的文件,它运行得非常好。