douduan2272 2016-02-08 16:19
浏览 36
已采纳

symfony2 ajax调用始终返回null数据

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条回答 默认 最新

  • doubang9906 2016-02-08 16:53
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵