需要更改PHP响应的状态代码

时间:2018-04-13 20:34:04

标签: php jquery ajax http-headers

如果HTTP Status Code出现问题,我正在尝试更改PHP中的SQL Query,并HTTP Status Code通过AJAX来接收此jQuery registro.php }}

为什么不工作?这是<?php require_once('conexao.php'); // the connection to db $telefone = $_POST['telefone']; // get the tel number $celular = $_POST['celular']; // get the cel number $celular = str_replace(array("(",")","-"," "), "", $celular); $dddCelular = substr($celular, 0, 2); $celular = substr($celular, 2, 9); $telefone = str_replace(array("(",")","-"," "), "", $telefone); $dddTelefone = substr($telefone, 0, 2); $telefone = substr($telefone, 2, 8); // after split the region code I check if is equal to a specify region, in case 11 if($dddCelular == "11" || $dddTelefone == "11") { $sql = "INSERT INTO `bigdata` (`id`, `origem`, `nome`, `dddtelefone`, `telefone`, `dddcelular`, `celular`, `email`, `obsadm`, `observacoes`, `timestampcadastro`) VALUES (NULL, 'renovacnh', '".utf8_decode($nome)."', '".$dddTelefone."', '".$telefone."', '".$dddCelular."', '".$celular."', '".$email."', '".$horario."', '".$observacoes."', CURRENT_TIMESTAMP);"; if(mysqli_query($conn, $sql)) { mysqli_close($conn); echo "OK"; http_response_code(200); // the query is ok! so, change to 200 }; } else { // var_dump(http_response_code()); // int(200) - default http_response_code(503); // set a new code // var_dump(http_response_code()); // int(503) - new code echo "ERROR"; } 文件内容:

AJAX

这是我的<script> function pixel(data) { console.log(data); if(data == "OK") { alert("It works!"); } else { alert("Fail"); } } function envia() { $.ajax({ method: "POST", url : "php/registro.php", data: { "celular": "(21) 95494-3943", // the phone is wrong region code, not 11 "telefone": "(21) 4494-3943", // the cel is wrong region code, not 11 }, success: function (data,xhr) { pixel(data); console.log(xhr); // trow success }, error: function (error,xhr) { console.log(error); console.log(xhr); } }); } envia(); </script> 脚本:

AJAX

所以最后,我试图通过if()检查响应是否等于“OK”,并且HTTP Status Code以某种方式失败。并且根本没有验证为OK响应。所以我尝试使用AJAX以某种方式检查@Override public void deleteEntry(long id) { String deleteQuery = "DELETE FROM company WHERE id = ?"; jdbcTemplate.update(deleteQuery, id); } 响应。

有什么想法吗?谢谢!

0 个答案:

没有答案