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'
    
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站