繁体   English   中英

在jQuery AJAX JSON PHP POST上找不到404

[英]404 Not Found on Jquery AJAX JSON PHP POST

我正在尝试将一些JSON数据发布到本地主机,并且不断收到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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM