0

我有一个这样标记的表格:

<fieldset>
    <form action="testuser.php" method="post" id="thisisatest">
        <input type = "hidden" name = "req" value = "REQUSERSIGNIN">
        <input type = "hidden" name = "platform" value = "WEB">    
        <label>Email</label> <input type = "text" name = "useremail" value = "" id="userLabel"> <br />
        <label>Pass</label><input type = "password" name = "userpass" value = "" id="userPassword"> 
        <br />
        <input type = "submit" value = "Submit">
    </form>
</fieldset>

我正在尝试像这样进行 ajax 调用:

$("#thisisatest").submit(function(){
    $.ajax({
        type: "POST",
        url: "http://localhost/api/apis/User.php?req=REQUSERSIGNIN",
        data: "useremail=" + $("#userLabel").val() + "&userpass" + $("#userPassword").val(),
        success: function(data) {
            console.log(data);
        }
    });
    return false;
});

我提交的php文件的函数是ff:

public function userSignIn ($request) {     

    $result[COCNST::TYPE] = COCNST::TYPELOGIN;

    // key info missing
    if ( empty($request['useremail']) || empty($request['userpass'])) {     
        $result[COCNST::STATUS] = "NO";
        $result[COCNST::MESSAGE] = COCNST::ERR_NOEMAILORPASS;
        $result[COCNST::MESSAGEUSER] = COCNST::ERRMSG_NOEMAILORPASS;                    
        return $result;             
    }

    $response = $this->db->userSignIn($request['useremail']);

    $pass = trim($request['userpass']);                                             
    // login does not exist or password incorrect           
    if ( ! is_numeric($response['user']['id']) || crypt($pass, $response['user']['pass']) != $response['user']['pass']) {
        $result[COCNST::STATUS] = COCNST::STATUS_NO;
        $result[COCNST::MESSAGE] = COCNST::ERRMSG_LOGINFAILED;
        $result[COCNST::MESSAGEUSER] = COCNST::ERRMSG_LOGINFAILED;                                      
        return $result;
    } 


    if ($response['user']['signupstatus'] == COCNST::USERSIGNUPEMAILSTATUS_EMAILNOTCONFIRMED) {         
        $result[COCNST::STATUS] = COCNST::STATUS_NO;
        $result[COCNST::MESSAGE] = COCNST::ERR_EMAILNOTCONFIRMED;
        $result[COCNST::MESSAGEUSER] = COCNST::ERRMSG_EMAILNOTCONFIRMED;                    
        return $result;             

    }


    $result[COCNST::STATUS] = COCNST::STATUS_YES;
    $result[COCNST::MESSAGE] = COCNST::OK;
    $result[COCNST::MESSAGEUSER] = COCNST::MSG_LOGINYES;
    $result[COCNST::HANDLE] = $response['user']['handle'];
    $result[COCNST::UUIDUSER] = $response['user']['uuid'];  
    $result[COCNST::USERPIC] = $response['user']['picpath'];
    $result[COCNST::EMAIL] = $response['user']['email'];
    $result[COCNST::SIGNUPSTATUS] = $response['user']['signupstatus'];  
    $result[COCNST::TOKENLOGIN] = $response['user']['logintoken'];

    return $result;

}

然后这是通过 User.php 文件的 processpost 函数提供给我的:

public function processPost ($reqtype, $request) {

    $response = 0;  

    // user confirm email. user confirm link
    // user reset request. user         

    switch ($reqtype) {
        case COCNST::REQUSERSIGNIN :
            $user = new COUser();
            $response = json_encode($user->userSignIn($request));           
            break;
                (rest of code)....
    }
}

当我在没有 ajax 的情况下提交表单时,它似乎可以工作,但我似乎无法通过 ajax 复制它。我哪里错了?

4

1 回答 1

1

请使用相对路径

url: "http://localhost/api/apis/User.php?req=REQUSERSIGNIN",

可能像(实际上取决于目录结构)

url: "/api/apis/User.php?req=REQUSERSIGNIN",

数据也将有括号 { }

请看这个例子

data: { name: "John", location: "Boston" }
于 2013-09-10T21:16:44.603 回答