php jquery .ajax 总是返回错误 - 数据被添加到数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18862066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:19:56  来源:igfitidea点击:

jquery .ajax always returns error - data being added to database

javascriptphpjqueryhtmlajax

提问by T0w3ntuM

I am trying to add users to a database using jquery ajax calls. The users get added just fine to the database, but the ajax always returns with error. I'm not sure how to retrieve the specific error either. Below is my code, form, php, and jquery.

我正在尝试使用 jquery ajax 调用将用户添加到数据库中。用户被添加到数据库中就好了,但 ajax 总是返回错误。我也不知道如何检索特定错误。下面是我的代码、表单、php 和 jquery。

Here is the jquery

这是jquery

$(document).ready(function() {

    //ajax call for all forms. 
    $('.button').click(function() {
        var form = $(this).closest('form');
        $.ajax({
            type: "POST",
            url: form.attr('data'),
            dataType: 'json',
            data: form.serialize(),
            success: function (response) {
                alert('something');
            },
            error: function() {
                alert('fail');
            }
        });
    });
  });

Here is the PHP

这是PHP

<?php
include 'class_lib.php';

if(isset($_POST['username'])) {
    $user = new Users;
    $user->cleanInput($_POST['username'], $_POST['password']);
    if($user->insertUser()) {
        echo json_encode('true');
    } else {
        echo json_encode('false');
    }
}

Here is the HTML

这是 HTML

<div id='newUser' class='tool'>
    <h3>New User</h3>
    <form method='post' name='newUser' data='../php/newUser.php'>
        <span>Username</span><input type='text' name='username'><br>
        <span>Password</span><input type='password' name='password'>
        <input type='submit' name='submit' class='button' style='visibility: hidden'>
    </form>
    <span class='result'> </span>
</div>

回答by Vicer

@Musa, above you mentioned

@Musa,上面你提到了

My guess is its a parsing error, try removing dataType: 'json', and see if it works

我的猜测是解析错误,尝试删除 dataType: 'json',看看它是否有效

You absolutely solved the problem I was having! My ajax post request was similar to above and it just kept returning to the 'error' section. Although I checked using firebug, the status was 200(ok) and there were no errors.

你绝对解决了我遇到的问题!我的 ajax post 请求与上面类似,它只是一直返回到“错误”部分。虽然我使用 firebug 进行了检查,但状态为 200(ok) 并且没有错误。

removing 'dataType:json' solved this issue for me. Thanks a lot!

删除 'dataType:json' 为我解决了这个问题。非常感谢!

回答by T0w3ntuM

Turns out I had to add async: falseto the $.ajax function. It wasn't getting a response back from the php.

结果我不得不添加async: false到 $.ajax 函数中。它没有从 php.ini 得到响应。

回答by noderman

I know this is an old question but I have just run into a weird situation like this ( jquery ajax returns success when directly executed, but returns error when attached to button, even though server response is 200 OK)

我知道这是一个老问题,但我刚刚遇到了这样的奇怪情况(直接执行时 jquery ajax 返回成功,但附加到按钮时返回错误,即使服务器响应为 200 OK

And found that having the button inside the formtags caused JQuery to always return error. Simply changing the formtags to divsolved the problem.

form并发现在标签内放置按钮会导致 JQuery 总是返回错误。只需更改form标签即可div解决问题。

I believe JQuery assumes the communication should be form encoded, even though you say it is application/json.

我相信 JQuery 假设通信应该是形式编码的,即使你说它是application/json.

Try moving your button outside your form and see what happens...

尝试将按钮移到表单之外,看看会发生什么......

回答by GuilhermePinto

I had the same problem and discovery there. All the time the problem is the version of my jQuery, I had use jquery version (jquery-1.10.2.js)but this version is not Ajax stablish. So, I change version for (jquery-1.8.2.js)and this miracle heppened.

我在那里遇到了同样的问题和发现。问题一直是我的 jQuery 版本,我曾经使用过 jquery 版本(jquery-1.10.2.js)但这个版本不是 Ajax 稳定版。所以,我更改了(jquery-1.8.2.js) 的版本,这个奇迹发生了。

Good Luck Guy!

好运的家伙!

回答by skmasq

You should specify status Code 200 for successful response.

您应该为成功响应指定状态代码 200。

<?php
http_response_code(200);
?>

See here: http://php.net/manual/en/function.http-response-code.php

见这里:http: //php.net/manual/en/function.http-response-code.php