在使用 POSTMAN 时,AJAX POST 请求不起作用
Posted
技术标签:
【中文标题】在使用 POSTMAN 时,AJAX POST 请求不起作用【英文标题】:AJAX POST request not working while working on POSTMAN 【发布时间】:2021-12-24 01:34:58 【问题描述】:我正在尝试通过 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"
]
尽管我不确定是否有必要,这是我的 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,
]);
我看不出错误来自哪里,知道吗?
【问题讨论】:
【参考方案1】:因为您使用的是“application/json”内容类型,所以试试这个
contentType: "application/json",
dataType: 'json',
data: JSON.stringify(quote),
....
或删除
contentType: "application/json",
【讨论】:
确实,我正要删除我的问题。谢谢 !那我就不说了:) 不客气!请不要忘记接受答案,因为它很有帮助。否则它将被否决。 是的,我想要,但我仍然需要等待。我当然会以上是关于在使用 POSTMAN 时,AJAX POST 请求不起作用的主要内容,如果未能解决你的问题,请参考以下文章