jquery - 在使用 POSTMAN 时,AJAX POST 请求不起作用
问题描述
我正在尝试通过 AJAX 请求发布数据。这是执行此操作的代码:
console.log(quote)
$.ajax({
url: '{{ path('set_products_in_db') }}',
type: 'POST',
contentType: "application/json",
dataType: 'json',
data: quote,
success: function (data, status) {
result = data;
if (status === 'success') {
console.log(data)
}
}
})
ajax 请求返回 500 错误。如您所见,我之前做了一个 console.log 来查看我发送的内容。该对象似乎是正确的。我从检查窗口复制对象并将其粘贴到 Postman 中,它正在工作。我想这个问题来自我的 Ajax 请求,仍然是我发送的对象:
{
"name": "Test",
"firstname": "Test2",
"company": "Company",
"phone": "123456789",
"email": "test@gmail.com",
"TVA": "123456789",
"street": "Rue du test",
"number": "3",
"cpt": "A",
"postalCode": "1000",
"city": "Paris",
"country": "France",
"products": [
{
"name": "Produit1",
"price": "10",
"reference": "Ref1"
},
{
"name": "Produit2",
"price": "15",
"reference": "Ref2"
}
]
}
Eventough我不确定是否有必要,这是我在 PHP 中的后端
public function setProducts(Request $request)
{
$productsRepo = $this->getDoctrine()
->getRepository(Product::class);
$allproducts = $productsRepo->findAll();
$allref = array_map(function ($e) {
return is_object($e) ? $e->getReference() : $e['Reference'];
}, $allproducts);
dump($allproducts);
dump(array_column($allproducts, 'reference'));
$JSONrequest = json_decode($request->getContent());
$products = $JSONrequest->products;
$name = $JSONrequest->name;
$firstname = $JSONrequest->firstname;
$company = $JSONrequest->company;
$phone = $JSONrequest->phone;
$email = $JSONrequest->email;
$street = $JSONrequest->street;
$number = $JSONrequest->number;
$cpt = $JSONrequest->cpt;
$postalCode = $JSONrequest->postalCode;
$city = $JSONrequest->city;
$country = $JSONrequest->country;
$TVA = $JSONrequest->TVA;
$entityManager = $this->getDoctrine()->getManager();
$repoGathering = $this->getDoctrine()
->getRepository(Gathering::class);
$gathering = $repoGathering->findOneBy([
'id' => 1,
]);
foreach ($products as $product) {
$nameProduct = $product->name;
$price = $product->price;
$reference = $product->reference;
if (!in_array($reference, $allref)) {
$newProduct = new Product();
$newProduct->setName($nameProduct);
$newProduct->setPrice($price);
$newProduct->setReference($reference);
$newProduct->setGathering($gathering);
$entityManager->persist($newProduct);
$entityManager->flush();
}
}
// return $this->render('product/test.html.twig', [
//
// ]);
return $this->json([
'products' => $products,
'name' => $name,
'firstname' => $firstname,
'company' => $company,
'phone' => $phone,
'email' => $email,
'street' => $street,
'number' => $number,
'cpt' => $cpt,
'postalCode' => $postalCode,
'city' => $city,
'country' => $country,
'TVA' => $TVA,
]);
}
我看不出错误来自哪里,知道吗?
解决方案
由于您使用的是“application/json”内容类型,请试试这个
contentType: "application/json",
dataType: 'json',
data: JSON.stringify(quote),
....
或删除
contentType: "application/json",
推荐阅读
- uitableview - 没有 Xamarin.Forms 的 UITableView 标题背景颜色
- php - 将 Base64 ZIP 字符串另存为 ZIP 文件 - PHP
- excel - 嵌套 IF 公式失败
- azure - 是否可以将 devcontainer 部署为私有 ACI?
- python - keras seq2seq 模型的注意力层
- django - Django imagefield 不显示在模板上
- ruby-on-rails - 在不同的类别中显示相同的任务
- c++ - 具有默认值的 const 引用参数
- react-native - React Native:如何在覆盖视图中响应事件,然后将事件传递到下一层视图
- visual-studio-code - Visual Studio Code - 空白窗口无边框