为什么我的jQuery ajax POST不工作的GET工作很好


why my jQuery ajax POST does not work by GET works fine?

我写了一个jquery ajax与POST,但它没有工作。经过几个小时的搜索,我发现通过更改为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;

运行这段代码后,显示"not found"。我搜索了整个网络,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>

一个AJAX请求需要一个web服务器。它不会只在你的本地笔记本电脑上运行,这就解释了为什么POST没有做任何事情。

为了解决这个问题,你可以在笔记本电脑上安装一个带有PHP的web服务器。WAMP和XAMPP是我想到的两个包

试过之后,

    if(!isset($_POST['name'])) {
        $_POST['name'] = "Error";
    }
    $name = $_POST['name'];
    echo "Welcome ". $name;

经过几个小时的努力,我终于找到了我的问题。因为你可能遇到过这种问题,我的问题有一个真正简单的解决方案,因为我在PhpStorm上工作,我在PhpStorm上安装了bitNami XAMP的php,所以我的php脚本工作得很好,直到Post请求,其中它需要一个web服务器作为Jay提到它,解决方案是:

我复制我的文件到XAMP, htdocs文件夹,它运行得很好