【问题标题】:symfony2 ajax call always return null datasymfony2 ajax 调用总是返回空数据
【发布时间】:2016-05-18 09:22:13
【问题描述】:

我正在使用 Symfony 2.7,我从数据库中查询信息并将其显示在页面上,但是我想通过单击按钮通过 Ajax 调用通过 api 连接获取当前值,但我总是从 Ajax 控制器获得空响应.

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

我的控制器:

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

控制台日志

 Object { sucess: true, data: null }

【问题讨论】:

    标签: php ajax symfony


    【解决方案1】:

    因为您的数据对象没有data 键,所以您无法通过$request-&gt;request-&gt;get('data'); 检索它

    要获取整个对象,请使用$data = $request-&gt;request-&gt;all();

    您的代码中有很多错误。
    您在 productNr 而不是 ProductNr 中推送值。
    您有许多具有相同 id 的元素(一个 id 是 uniq,您必须使用类)。

    编辑

    问题在于您发送的数据的格式。 要发送 {"data":["1K0615301M1","1K0615301M2"]} 之类的对象,请使用:

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

    在发送数据之前使用JSON.stringify 序列化数据。
    How do I POST an array of objects with $.ajax (jQuery or Zepto)

    【讨论】:

    • 我在代码中确实有请求参数...但我仍然得到来自控制器的 null 响应 $data = $request-&gt;request-&gt;all()json_decode($request-&gt;getContent(),true
    • 尝试删除 contentType
    • 我怎样才能添加“数据”键,以便更容易在控制器中获取值
    • 我也试过ajaxRequest = $.ajax({ url: " {{ path('frontend_api_product') }}", type: "post", processData: false, data:'asdf', success: function (data) { console.log(data); } });,但我仍然得到Null响应
    • 查看我的答案的变化,代码现在运行良好。
    【解决方案2】:

    更改
    data: ProductNr,

    data:{data:ProductNr}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      • 2017-05-18
      • 1970-01-01
      • 2013-12-04
      • 2019-12-02
      • 1970-01-01
      相关资源
      最近更新 更多