0

I am using Symfony 2.7 and I query the information from the database and show it on the page however i want to get the current values via api connection through Ajax call by clicking the button but I always get the null response from Ajax Controller.

<div>
    <p id="product">1K0615301M1</p>
    <p id="product">1K0615301M2</p>   
    <input type="button" id="submit" value="Check"/>
</div>

<script>
     $(document).ready(function(){
         $('#submit').click(function(event) {
             var productNr = [];
             $('#product').each(function() {
                 productNr.push($(this).html());
             });
             console.log(ProductNr); // value of ProductNr
             var ajaxRequest;
             event.preventDefault();

             ajaxRequest = $.ajax({
                 url: " {{ path('frontend_api_product') }}",
                 type: "post",
                 processData: false,
                 contentType: 'application/json; charset=UTF-8',
                 data: ProductNr,
                 success: function (data) {
                     console.log(data);
                 }
             });
         });
     });
 </script>

My Controller:

public function AjaxAction(Request $request)
{
    $sparepart = $request->request->get('data');

    if ($request->isXMLHttpRequest()) {
        return new JsonResponse(array(
            'sucess'=> true,
            'data' => $sparepart
        ));
    }
    return new Response('This is not ajax!', 400);
}

Console.log

 Object { sucess: true, data: null }

2 Answers 2

1

Because your data object has no data key, you cannot retrieve it by doing $request->request->get('data');

To get the whole object, use $data = $request->request->all();

There is many errors in your code.
You are pushing values in productNr instead of ProductNr.
You have many elements with the same id (An id is uniq, you have to use classes).

EDIT

The problem is coming the format of the data your are sending. To send an object like {"data":["1K0615301M1","1K0615301M2"]} , use:

var ProductNr = { data: [] };
var ajaxRequest;

$('.product').each(function() {
    var product = $(this).text();
    ProductNr.data.push(product);
});
ajaxRequest = $.ajax({
    url: "/ajax",
    type: "POST",
    data: JSON.stringify(ProductNr),
    processData: false,
    success: function (data) {
        console.log(data);
    }
});

Use JSON.stringify to serialise data before send it.
See How do I POST an array of objects with $.ajax (jQuery or Zepto)

Sign up to request clarification or add additional context in comments.

14 Comments

I do have Request Param in the code... But I still get null response from the controller for both $data = $request->request->all() and json_decode($request->getContent(),true
Try to remove the contentType
also how can i add 'data' key so that its easier to get the value in the Controller
I have also tried with ajaxRequest = $.ajax({ url: " {{ path('frontend_api_product') }}", type: "post", processData: false, data:'asdf', success: function (data) { console.log(data); } }); and I still get Null response
See changes in my answer, the code is working well now.
|
0

Change
data: ProductNr,
to

data:{data:ProductNr}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.