weixin_33739541 2017-07-27 08:39 采纳率: 0%
浏览 22

Ajax + php json回答错误

I'v got a problem with my site registration form. Getting 'null' from server, using json format of exchange.

Code:

JS part of code:

$("#customer-form").submit(function() { 
        var form_data = {
          customer_name: $("#customer_name").val(),
          customer_dir: $("#customer_dir").val(),
          confirm_dir: $("#confirm_dir").val(),
          customer_fname: $("#customer_fname").val(),
          customer_address: $("#customer_address").val(),
          customer_inn: $("#customer_inn").val(),
          customer_kpp: $("#customer_kpp").val(),
          customer_ogrn: $("#customer_ogrn").val(),
          customer_okpo: $("#customer_okpo").val(),
        }; 
        $.ajax({
        type: "POST",
        url: "customer_form.php",
        data: form_data,
        dataType: "json",
        success: function(data) { 
          console.log(data.status);
                },      
        error: function(xhr, status) {
               console.log("Error"+" "+xhr.responseText +" "+ status)
               }
           }); 
      });

PHP

<?php   
    $customer_fname=trim($_POST["customer_fname"]);
    $customer_fname=strip_tags($customer_fname); 

    $confirm_dir=trim($_POST["confirm_dir"]);
    $confirm_dir=strip_tags($confirm_dir);
    $confirm_dir = md5($confirm_dir);

    $customer_name=trim($_POST["customer_name"]);
    $customer_name=strip_tags($customer_name);

    $customer_dir = trim($_POST["customer_dir"]);
    $customer_dir=strip_tags($customer_dir);
    $customer_dir=md5($customer_dir);

    $customer_address=trim($_POST["customer_address"]);
    $customer_address=strip_tags($customer_address);

    $customer_inn=trim($_POST["customer_inn"]);
    $customer_inn=strip_tags($customer_inn);

    $customer_kpp=trim($_POST["customer_kpp"]);
    $customer_kpp=strip_tags($customer_kpp);

    $customer_ogrn=trim($_POST["customer_ogrn"]);
    $customer_ogrn=strip_tags($customer_ogrn);

    $customer_okpo=trim($_POST["customer_okpo"]);
    $customer_okpo=strip_tags($customer_okpo);

    if($customer_dir==$confirm_dir){ 
        $con = new mysqli("localhost", "root", "", "berezka");
        $sql = "SELECT * FROM CUSTOMER where customer_name='".$customer_name."'";
        $result = $con->query($sql);

        if($row = $result->fetch_assoc()){
           $response = array("status"=>"Login has been already given");
           echo json_encode($response);
        } 
        else{ 
            $sql_2 = "INSERT INTO customer (`customer_id`, `customer_fname`, `customer_name`, `customer_dir`, `customer_address`, `customer_inn`, `customer_kpp`, `customer_ogrn`, `customer_okpo`) VALUES ('', '$customer_fname', '$customer_name', '$customer_dir', '$customer_address', '$customer_inn', '$customer_kpp', '$customer_ogrn', '$customer_okpo')";
            mysqli_query($con, $sql_2);
            $response = array("status"=>"Registration is ok");
            echo json_encode($response);
           // mysqli_free_result($result);
           // mysqli_close($con);
       }  
    }  
    else{   
        $response = array("status"=>"Password doesnt match");
        echo json_encode($response);   
    } 
?>

Here is my error in firefox firegub or Chrome console: Uncaught TypeError: Cannot read property 'status' of null

I was trying to use JSON.Parse or etc. Tried to make json message by object,array or another format.

  • 写回答

1条回答 默认 最新

  • weixin_33721344 2017-07-27 09:30
    关注

    Please add header info:

    header('Content-Type:application/json'); 
    

    before sending the response.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测