dongliang1941 2015-03-26 16:13
浏览 98

在Jquery AJAX JSON PHP POST上找不到404

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
</div>
  • 写回答

1条回答 默认 最新

  • doutuobao4004 2015-03-26 16:17
    关注

    have you ensure with correct url in ajax?

    maybe not thi:

    url: 'ajax.php'
    

    but this:

    url: 'api/ajax.php'
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大