404在Jquery AJAX JSON PHP POST上找不到


404 Not Found on Jquery AJAX JSON PHP POST

我正试图将一些JSON数据POST到本地主机,但我一直收到一个404 Not Found错误,这很奇怪,因为php文件位于脚本中指定的正确位置。我将感谢任何有此经验的人的任何反馈。我收到这个错误是因为服务器不知什么原因找不到ajax.php文件吗?

<div class="container">
            <div class="header">
                <h3 class="text-muted">AJAX JSON Data</h3>
            </div>
            <div id="data-div">
                <form method="post" action="api/ajax.php" class="ajax">
                    <p><label for="firstname" class="contact-input-text">First Name</label> <br/>
                    <input id="first-name" name="firstname" type="text" maxlength="30" autofocus /></p><p><label for="lastName" class="contact-input-text">Last Name</label> <br/>
                    <input id="last-name" name="lastname" type="text" maxlength="30" autofocus /></p>
                    <p><input type="submit" id="submit-button" class="contact-input-text" value="submit" /></p>
                </form>
            </div>
        </div>
<script>
$('form.ajax').on('submit', function(){
            
            var jsondata = {};
            $(this).find('[name]').each(function(i, data){
                console.log(data);
                var that = $(this); 
                var key = that.attr('name');
                var value = that.val();
                jsondata[key] = value;
            });
            console.log(jsondata);
            $.ajax({
                    type: 'POST',
                    url: 'ajax.php',
                    dataType: 'json',
                    data: jsondata,
                    success: function(response){
                        console.log(response);
                    },
                    error: function(xhr){
                    console.log(xhr);
                    }
                });
            return false;
</script>
 

这是ajax.php文件。。。。

<?php 
	if(isset($_POST['submit'])) {
		$file = "data.json";
		$json_string = json_encode($_POST,JSON_PRETTY_PRINT);
		file_put_contents($file,$json_string,FILE_APPEND);
	}
?>

这是目录结构:

index.html (contains the form input fields and the ajax request)
ajax.php
/styles
/images

您是否确保ajax中的url正确?

也许不是这样:

url: 'ajax.php'

但是这个:

url: 'api/ajax.php'