【发布时间】:2017-07-27 08:39:08
【问题描述】:
我的网站注册表有问题。使用 json 格式的交换从服务器获取“null”。
代码:
JS部分代码:
$("#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);
}
?>
这是我在 firefox firegub 或 Chrome 控制台中的错误: 未捕获的类型错误:无法读取 null 的属性“状态”
我试图使用 JSON.Parse 等。试图通过对象、数组或其他格式制作 json 消息。
【问题讨论】:
-
我的数据库没问题,它插入正确,我在 php 页面上得到了正确的消息。但我无法在 ajax 方面得到正确的响应
-
添加头信息 header('Content-Type:application/json'); echo json_encode($response);
-
var form_data = { customer_name: $("#customer_name").val(), customer_dir: $("#customer_dir").val(), confirm_dir: $("#confirm_dir").val(),...等。如果这些都是客户表单中的所有字段,您可以放弃所有这些繁琐的重复代码,只需在 ajax 选项中写入data: $(this).serialize()。 jQuery 会为你处理好它。详情请见api.jquery.com/serialize -
strip_tags()不会让你免于 SQL 注入——这段代码真的很不安全。在将其替换为绑定参数之前,请勿将其发布到网络上。