ajax呼叫返回网络错误>> 404

时间:2015-07-31 10:21:59

标签: javascript php ajax

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 个答案:

答案 0 :(得分:0)

404表示找不到您尝试覆盖的网址。我怀疑,根据您的ajax网址上方的评论,它应该是http://127.0.0.1/projects/register2.php而不是http://127.0.0.1/projects/register.php

答案 1 :(得分:0)

404错误表示调用尝试获取的资源不存在,因此您可能希望检查该调用的去向以及尝试返回的内容。

这里的调用也是一个特定的IP地址,所以我假设你在本地进行测试,但如果它返回404,那么你在那里设置的内容可能无法运行。

答案 2 :(得分:0)

php文件>>如下所示,Access头工作正常,没有访问权限,xdk模拟器返回网络错误

                <?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..
*/
?>