简体   繁体   中英

404 Not Found on Jquery AJAX JSON PHP POST

I'm trying to POST some JSON data to a local host and I keep getting a 404 Not Found error which is strange because the php file is located in the correct location as specified in the script. I would appreciate any feedback from anyone who has experience with this. Am I getting this error because the the server can not locate the ajax.php file for some unknown reason?

 <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> 

Here is the ajax.php file....

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

This is the directory structure :

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

have you ensure with correct url in ajax?

maybe not thi:

url: 'ajax.php'

but this:

url: 'api/ajax.php'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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