从php返回值到ajax成功和错误

时间:2015-11-08 16:54:25

标签: php jquery mysql ajax mongodb

我正在使用Jquery mobile& amp; php通过引用教程。但教程后端是mongoDB。我希望我的应用程序连接到PHP& MySQL的。

我对教程代码做了一些更改,我对php返回错误或成功感到困惑。

AJAX代码

 $.ajax({
            type: 'POST',
            url: BookIt.Settings.signUpUrl,
            data:"submitted=" + "1" + "&mobile=" + mobileNumber + "&firstName=" + firstName + "&lastName=" + lastName + "&password=" + password,
            dataType: "json",
            success: function (resp) {
            alert(resp);
                console.log("success");
                if (resp.success === true) {
                    $.mobile.navigate("signup-succeeded.html");
                    return;
                }
                if (resp.extras.msg) {
                    switch (resp.extras.msg) {
                        case BookIt.ApiMessages.DB_ERROR:
                        case BookIt.ApiMessages.COULD_NOT_CREATE_USER:
                            // TODO: Use a friendlier error message below.
                            $ctnErr.html("<p>Oops! BookIt had a problem and could not 2 register you.  Please try again in a few minutes.</p>");
                            $ctnErr.addClass("bi-ctn-err").slideDown();
                            break;
                        case BookIt.ApiMessages.MOBILENUMBER_ALREADY_EXISTS:
                            $ctnErr.html("<p>The mobile number that you provided is already registered.</p>");
                            $ctnErr.addClass("bi-ctn-err").slideDown();
                            $txtMobileNumber.addClass(invalidInputStyle);
                            break;
                    }
                }

            },
            error: function (e) {
                console.log(e.message);
                // TODO: Use a friendlier error message below.
                $ctnErr.html("<p>Oops! BookIt had a problem and could not register you.  Please try again in a few minutes.</p>");
                $ctnErr.addClass("bi-ctn-err").slideDown();
            }
        });

MY PHP返回JSON编码输出

echo json_encode("success");

API消息

var BookIt = BookIt || {};
BookIt.ApiMessages = BookIt.ApiMessages || {};
BookIt.ApiMessages.EMAIL_NOT_FOUND = 0;
BookIt.ApiMessages.INVALID_PWD = 1;
BookIt.ApiMessages.DB_ERROR = 2;
BookIt.ApiMessages.NOT_FOUND = 3;
BookIt.ApiMessages.EMAIL_ALREADY_EXISTS = 4;
BookIt.ApiMessages.COULD_NOT_CREATE_USER = 5;
BookIt.ApiMessages.PASSWORD_RESET_EXPIRED = 6;
BookIt.ApiMessages.PASSWORD_RESET_HASH_MISMATCH = 7;
BookIt.ApiMessages.PASSWORD_RESET_EMAIL_MISMATCH = 8;
BookIt.ApiMessages.COULD_NOT_RESET_PASSWORD = 9;
BookIt.ApiMessages.PASSWORD_CONFIRM_MISMATCH = 10;
BookIt.ApiMessages.MOBILENUMBER_ALREADY_EXISTS=11;
BookIt.ApiMessages.USERNAME_ALREADY_EXISTS=12;

我如何将错误/成功返回给这个ajax,所以它工作正常

Tutorial am referring

1 个答案:

答案 0 :(得分:1)

你正在检查

if (resp.success === true)

这意味着json数据的格式必须如下:

{"success":true}

由以下人员制作:

echo json_encode( ['success'=>true] ); // or array() instead of [] for PHP < 5.4

同样,对于错误(if (resp.extras.msg)),

echo json_encode( [ 'extras' => ['msg' => $errorcode] ] );

生成此JSON($ errorcode = 2(BookIt.ApiMessages.DB_ERROR))

{"extras":{"msg":2}}