PHP api发送HTTP_响应_代码(400),无论发生什么情况
我编写了一个简单的PHP crud api,无论我做什么,我都会得到PHP api发送HTTP_响应_代码(400),无论发生什么情况,php,ajax,rest,Php,Ajax,Rest,我编写了一个简单的PHP crud api,无论我做什么,我都会得到HTTP\u response\u code(400) 在api中,名为create.php的文件负责在数据库中插入新项,它检查从ajax接收的数据是否为空,然后继续创建,如果为空,它将发送HTTP\u响应\u代码(400) 但是,无论我做什么,它总是发送HTTP\u响应\u代码(400),即使数据不是空的 我认为问题首先来自ajax,但在调试之后,我发现facts中的ajax从表单中获取适当的数据并发送它 这是我的create
HTTP\u response\u code(400)
在api中,名为create.php的文件负责在数据库中插入新项,它检查从ajax接收的数据是否为空,然后继续创建,如果为空,它将发送HTTP\u响应\u代码(400)
但是,无论我做什么,它总是发送HTTP\u响应\u代码(400)
,即使数据不是空的
我认为问题首先来自ajax,但在调试之后,我发现facts中的ajax从表单中获取适当的数据并发送它
这是我的create.php文件
$db = $database->getConnection();
$consumable = new consumable($db);
//get json
$json = file_get_contents("php://input");
// get posted data
$data = json_decode($json);
// make sure data is not empty
if(
!empty($data->reference) &&
!empty($data->price) &&
!empty($data->description) &&
!empty($data->category_id) &&
!empty($data->quantity)
){
// set consumable property values
$consumable->reference = $data->reference;
$consumable->price = $data->price;
$consumable->description = $data->description;
$consumable->category_id = $data->category_id;
$consumable->quantity = $data->quantity;
$consumable->created = date('Y-m-d H:i:s');
// create the consumable
if($consumable->create()){
// set response code - 201 created
http_response_code(201);
// tell the user
echo json_encode(array("message" => "consumable was created."));
}
// if unable to create the consumable, tell the user
else{
// set response code - 503 service unavailable
http_response_code(503);
// tell the user
echo json_encode(array("message" => "Unable to create consumable."));
}
}
else{
// tell the user data is incomplete
// set response code - 400 bad request
//http_response_code(400);
// tell the user
echo json_encode(array("message" => "Unable to create consumable. Data is incomplete."));
echo json_encode($json);
}
下面是我的ajax:
$(document).on('submit', '#create-consumable-form', function(){
alert("submit");
// get form data
var form=$(this).serializeObject();
var form_data=JSON.stringify(form);
console.log('a',form);
console.log(form_data);
// submit form data to api
$.ajax({
url: "http://localhost:3000/consumable/create.php",
type : "POST",
contentType : 'application/json',
data : form_data,
success : function(result) {
// consumable was created, go back to consumables list
showconsumables();
},
error: function(xhr, resp, text) {
// show error to console
console.log(xhr, resp, text);
}
});
return false;
});
填写表单并提交后,而不是将条目添加到数据库并发送201 OK
后,显示以下错误:
jquery.js:2 OPTIONS http://localhost:3000/consumable/create.php 400 (Bad Request)
send @ jquery.js:2
ajax @ jquery.js:2
(anonymous) @ create-consumables.js:87
dispatch @ jquery.js:2
v.handle @ jquery.js:2
index.html:1 Access to XMLHttpRequest at 'http://localhost:3000/consumable/create.php' from origin 'http://localhost:5500' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
这是我的console.log的结果
a {reference: "BT3000", price: "10", quantity: "5", description: "description", category_id: "3"}
create-consumables.js:85 {"reference":"BT3000","price":"10","quantity":"5","description":"description","category_id":"3"}
奇怪的是,当我对我的create.php
文件中的HTTP\u response\u code(400)
行进行注释时,它工作得很好。有人知道这种行为的原因吗?尝试将header()放在create.php文件中:
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
如果无法解码,json_decode将返回null
。看来就是这样。可能需要对内容进行url\u解码
和/或stripslashes
才能解码。正如Ahmed所说,尝试输出$data
变量和json\u decode
和文件的输出获取内容(“php://input");代码>您很快就会看到错误
还要注意!空(0)
和!空(false)
返回true
。因此,如果变量的值为0或false,那么在这种情况下,它也将返回400。在您的示例中,这不是问题,但以后可能会成为问题。问题的原因是,我的标题中的内容类型是application/json
,我使用postman进行测试,我将其保留为默认的内容类型,即text
您能var\u dump($data)吗
就在你的if之前,让我们看看它说了什么?我忘了说我已经有了与你发布的标题相同的标题[Thu Sep 12 08:21:07 2019]::1:13610[400]:/consumer/create.php