douzhi8244 2015-11-08 16:54
浏览 54
已采纳

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

I am working on User authentication using Jquery mobile & php by referring a tutorial. But the tutorial back end is mongoDB. I want my application connected to PHP & mysql.

I have made some change to the tutorial code, i am confused with php return on error or success.

AJAX code

 $.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 return JSON encode output

echo json_encode("success");

API Message

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;

how i can return error/success to this ajax, so it work fine

Tutorial am referring

  • 写回答

1条回答 默认 最新

  • doutuan6158 2015-11-08 17:03
    关注

    You're checking

    if (resp.success === true)
    

    which means that the json data must be formatted like this:

    {"success":true}
    

    which is produced by:

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

    Similarly, for errors (if (resp.extras.msg)),

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

    produces this JSON (with $errorcode = 2 (BookIt.ApiMessages.DB_ERROR))

    {"extras":{"msg":2}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败