[英]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.