AJAX表单提交基础知识

时间:2013-09-10 21:13:47

标签: jquery ajax forms

我有一个标记的表格:

<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复制它。我哪里错了?

1 个答案:

答案 0 :(得分:1)

请使用

中的相对路径
url: "http://localhost/api/apis/User.php?req=REQUSERSIGNIN",

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

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

也会有数据{}

请看这个例子

data: { name: "John", location: "Boston" }