weixin_33737134 2013-09-10 21:13 采纳率: 0%
浏览 30

AJAX表单提交基础

I have a form marked up like so:

<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>

And I'm trying to do an ajax call like so:

$("#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;
});

my php file's function which does the submission is the 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;

}

And then this is being fed to me via the User.php file's processpost function:

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)....
    }
}

When I submit the form without ajax, it seems to work, but I can't seem to replicate it via ajax. Where am I going wrong?

  • 写回答

1条回答 默认 最新

  • 关注

    please use relative path in

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

    may be like (depends on the directory structure actually)

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

    also data will have paranthesis { }

    please see this example

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

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用