du248227 2016-10-26 19:40
浏览 61
已采纳

如何使用带有AngularJS和Laravel的$ http.delete将数组传递给PHP?

In my AngularJS Service I have an array of id's which I want to pass to a PHP server so it can delete them but somehow I keep on getting a 500/Internal Server Error response.

In my server log it's saying that the request is missing one argument which means the passing to the server wasn't successful.

I have something like this in my service:

destroySelected : function (ids) {
  // console.log(ids);
  // return $http.delete('/posts/destroySelected', ids);
  return $http({
    method: 'DELETE',
    url: '/posts/destroySelected/',
    headers: {'Content-Type': 'application/json;charset=utf-8'},
    data: {ids : ids}
  });
}

For my php controller I have this:

public function destroySelected($ids) {
    echo "<pre>" . var_export($ids, true) . "<pre>";
    die;
    return response()->json(Post::get());
}

my route:

Route::delete('posts/destroySelected/', 'PostController@destroySelected');

It's empty, but I wanted to double check that it's being passed successfully before I do anything else.

Can someone tell me what's going wrong here?

  • 写回答

1条回答 默认 最新

  • doulou1970 2016-10-26 19:46
    关注

    You didn't provided any data to the controller :)

    You have : url: '/posts/destroySelected/' and data: {ids : ids}

    So, your final url will be /posts/destroySelected/?ids=1 (for example)

    The parameter $ids on your request require an url route parameter (for example route /posts/destroySelected/{ids}) and not a query parameter (_GET/_POST)

    Solution:

            return $http({
                method: 'DELETE',
                url: '/posts/destroySelected/' + ids,
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            });
    

    To get the query parameter (_GET), you can use:

    <?php
    public function destroySelected(Request $request) {
        $ids = $request->input('ids'); // will get the value of $_REQUEST['ids'] ($_GET or $_POST)
        // var_dump($request->all()); for all values
    
        echo "<pre>" . var_export($ids, true) . "<pre>";
        die;
        return response()->json(Post::get());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决