2015-05-04 38 views
1

我看过很多帖子在这里和这个错误的其他地方没有成功解决我的错误。在控制台中,表单数据按照预期通过json发送到我的php处理页面,php过程没有返回任何错误,所以我相信这个过程正在完成。我已经检查的结果被格式化的方式并不能看到什么不对劲的地方,虽然我在阿贾克斯/ JSON没有专家,所以我可能是错的,这是我的代码php json返回在响应中创建语法错误

jQuery代码

<script type="text/javascript"> 

$(document).ready(function() { 

    $("#admin_login_but").click(function() { 

    if($("#admin_name").val()=="" || $("#admin_password").val()=="" || $("#admin_email").val()=="") { 

    $(".adminloginError").html("Please enter Admin Name, Admin Email and Admin Password"); 

     return false; 

    }else{ 


    $(".adminloginError").html('<img src="image/ajax-loader.gif" width="16" height="16" alt=""/>'); 
    var adminName=$("#admin_name").val(); 
    var adminPassword=$("#admin_password").val(); 
    var adminEmail=$("#admin_email").val(); 

$.post("includes/admin_login.inc.php",{admin_name:adminName,admin_password:adminPassword, admin_email:adminEmail},function(json) { 
    if(json.result === "success") { 
     $(".adminloginError").html("Welcome "+adminName+"!"); 

     setTimeout(function(){ 
       window.location= "admin_secure.php"; 
     },2000);       

    }else{ 
     $(".adminloginError").html(json.message); 
    } 
}); 
    } 

这里是我的PHP处理代码

<?php 
include_once 'functions.php'; 
include_once 'db_connect.php'; 

header("Content-Type: application/json"); //this will tell the browser to send a json object back to client not text/html (as default) 

//convert variable (array) into a JSON object 

function result($var){ 
    echo json_encode($var); 
    exit(); 
} 

sec_session_start(); 

error_reporting(E_ALL); ini_set('display_errors', 1); 


//check if surname is empty 
    if (isset($_POST["admin_login_but"])) { 

//check if first_name is empty 
    if (empty($_POST["admin_name"])) { 

     $response = array('result'=>'fail', 'message' => 'Missing Admin Name'); 
     result($response); 

    }else{ 

// if not empty sanitize first_name input 
    $admin_name = filter_input(INPUT_POST, 'admin_name', FILTER_SANITIZE_STRING); 

    } 


//Check if email is empty and santize and validate email 
     if (empty($_POST['admin_email'])) { 

     $response = array('result'=>'fail', 'message' => 'Missing Admin Email'); 
     result($response); 

     }else{ 

     $admin_email = filter_var($_POST['admin_email'], FILTER_SANITIZE_EMAIL); 
} 

     if (!filter_var($admin_email, FILTER_VALIDATE_EMAIL)) { 

     $response = array('result'=>'fail', 'message' => 'The Email is not in a Valid Email Format!'); 
     result($response); 

     } 



//check if register password input is empty 
    if (empty($_POST["admin_password"])) { 

     $response = array('result'=>'fail', 'message' => 'Missing Admin Password'); 
     result($response); 

    } else { 
//Sanitize the data passed in 'password' 
    $admin_password = filter_input(INPUT_POST, 'admin_password', FILTER_SANITIZE_STRING); 
} 

    //validate the data passed in 'password' 
    if (!preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $admin_password)) { 

     $response = array('result'=>'fail', 'message' => 'Password is in the Wrong Format!'); 
     result($response); 

     } 


//query database 
$results = mysqli_query($mysqli, "SELECT * FROM admin WHERE name = '$admin_name' AND email = '$admin_email' AND hash = '$admin_password'"); 

// Check if SQL query had erors 
if(!$results){ 

     $response = array('result'=>'fail', 'message' => 'sql error: ' . mysqli_error($mysqli)); 
     result($response); 
} 

// If query was successfull and there are rows do this: 
if (mysqli_num_rows($results)>0){ 

    $_GET['name'] = $admin_name; 

    $response = array('result'=>'success', 'message' => 'User is Authenticated'); 
    result($response); 

} else { 

    $response = array('result'=>'fail', 'message' => 'User Authentication Failed'); 
    result($response); 

} 
} 

?> 

我无法弄清楚如何解决这个问题。任何人都可以帮助请

+0

可能重复:意外结束数据在JSON数据的第1行第1列](http://stackoverflow.com/questions/29291222/json-parse-error-unexpected-end-of-data-at-line-1-column-1-of- json-data) –

+0

我已经看过这个,它侧重于jQuery脚本,我不相信我的问题是与jQuery的,但如何PHP返回JSON和你提到的问题不涉及,就像我说过的那样搜索全部解决方案,我只是在这里作为最后的手段发布 – Tiny

+0

是否检查过脚本开始或结尾没有空行/换行符? (如果是的话,它会创建httpheaders而不是你的json) – FeedTheWeb

回答

0

我想通了,我还没有把其他的选择在我的PHP结束了初始后检查

if(!isset($_POST)) { 

//all the processing code 


}else{ 
    $response = array('result'=>'success', 'message' => 'Post is Empty'); 
    result($response); 
} 
[JSON解析错误的