Home > Mobile >  AJAX POST request not working while working on POSTMAN
AJAX POST request not working while working on POSTMAN

Time:11-12

I'm trying to POST data via an AJAX request. Here is the code to do so:

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)
         }
         }
         })

The ajax request returns an 500 error. As you can see, I do a console.log before to see what I send. The object seems correct. I copy the object from the inspection window and paste it in Postman and it's working. I guess then that the issue is coming from my Ajax Request, still here the object I'm sending:

{
    "name": "Test",
    "firstname": "Test2",
    "company": "Company",
    "phone": "123456789",
    "email": "[email protected]",
    "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 I'm not sure it's necessary, here is my backend in 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,
        ]);
    }

I don't see where the error comes from, any idea ?

CodePudding user response:

since you are using "application/json" content type, try this

         contentType: "application/json",
         dataType: 'json',
         data: JSON.stringify(quote),
         ....

or remove

 contentType: "application/json",
  • Related