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());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备