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}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)