dongqi19827 2015-07-31 10:21
浏览 117
已采纳

ajax调用返回网络错误>> 404

if(activePage.attr('id') === 'register_page') {
    $(document).on('click', '#submit', function() { // catch the form's submit event
        if($('#fname').val().length > 3 && $('#lname').val().length > 3 && $('#username').val().length > 4 && $('#email').val().length > 5 ){

            userHandler.username = $('#username').val();

            // Send data to server through the Ajax call
            //$.post( "http://127.0.0.1/projects/register2.php", $( "#register-user" ).serialize() );

            // action is functionality we want to call and outputJSON is our data
            $.ajax({
            url: 'http://127.0.0.1/projects/services/register.php',
            data: {action : 'registration', formData : $('#register-user').serialize()},
            type: 'post',                  
            async: true,
            contentType: "application/json; charset=utf-8",
            dataType: 'JSON ',
            beforeSend: function() {
                    // This callback function will trigger before data is sent
                    $.mobile.loading('show'); // show Ajax spinner
                },
                complete: function() {
                    // trigger on data sent/received complete   
                    $.mobile.loading('hide'); // hide Ajax spinner
                },
                success: function (result) {
                    // Check if registration successful
                    if(result.status == 'success') {
                        userHandler.status = result.status;
                        $.mobile.changePage("#categories");                        
                    } else if(result.status == 'failure'){
                         $("#register-error").text("System failure... please try again!").show().fadeOut(3000);
                    }
                    else if(result.status == 'taken'){
                        alert('Username or email is taken!');
                    }
                },
                error: function (xhr, status, error ) {
                    // This callback function will trigger on unsuccessful action               
                    //alert('Network error has occurred please try again!'); >> kanyagia hapa
                    $("#register-error").text(xhr.status+ "Network error... please check and try agin!").show().fadeOut(3000);
                }
            });   
  • 写回答

3条回答 默认 最新

  • doupao1978 2015-08-03 07:04
    关注

    The php file>> as shown below with Access headers works fine, without the access, the xdk emulator returns network error

                    <?php
          header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PUT');
    require './services/config.php';
    
    if(!empty($_POST))
    {
        $errors=0;
        if(empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['username'])
                || empty($_POST['email']) || empty($_POST['pword']))
        {
            $errors++;
            $error_msg="Please fill all the fields";
            $status =(true);
           echo '{"empty":'.json_encode($status).'}';
        }
        //pick all the data
        $fname=$_POST['fname'];
        $lname=$_POST['lname'];
        $usern=$_POST['username'];
        $email=$_POST['email'];
        $pass=$_POST['pword'];
        $role=$_POST['role'];
    
        //encrypt pw
        $encrypt_pw=  md5($pass);
        //check if errors are still 0 and check user existence
        if($errors==0)
        {
            $query="SELECT * FROM users WHERE username=:aa OR email=:bb";
            $query_params=array(
                ':aa'=>$usern,
                ':bb'=>$email
            );
    
            try{
                $stmt=$db->prepare($query);
                $result=$stmt->execute($query_params);
            } catch (Exception $ex) {
                $errors++;
                $error_msg="Server failure... please try again";
                 $status =("0");
                  echo '{"status":'.json_encode($status).'}';
            }
            $row=$stmt->fetch();
            if($row)
            {
               $errors++;
               $error_msg="Username or email already exists"; 
               $status =(true);
                echo '{"exists":'.json_encode($status).'}';
            }
            //if errors still 0
            if($errors==0)
            {
                $query="INSERT INTO users(fname, lname, username, email, password, role, created_at) VALUES(:aa, :bb, :cc, :dd, :ee,:ff, NOW())";
                $query_params=array(
                    ':aa'=>$fname,
                    ':bb'=>$lname,
                    ':cc'=>$usern,
                    ':dd'=>$email,
                    ':ee'=>$encrypt_pw,
                    ':ff'=>$role
                );
    
                try{
                    $stmt=$db->prepare($query);
                    $result=$stmt->execute($query_params);
                } catch (Exception $ex) {
                    $errors++;
                    $error_msg="Server failure... please try again";
                    $status = array("status" => "failure");
                     echo json_encode($status);
                }
    
                if($errors==0)
                {
                    //registration successful
                    $success_msg="You registered";
                    $status =(true);
                     echo '{"success":'.json_encode($status).'}';
                }
            }
    
        }
    
    }
    /*
    some interface here..
    */
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥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 乘性高斯噪声在深度学习网络中的应用