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

时间:2021-10-12 09:58:08

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.

在我的AngularJS服务中,我有一个id的数组,我想传递给PHP服务器,所以它可以删除它们,但不知怎的,我继续获得500 /内部服务器错误响应。

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:

对于我的php控制器,我有这个:

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 个解决方案

#1


1  

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

你没有向控制器提供任何数据:)

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

你有:url:'/ posts / destroySelected /'和数据:{ids:ids}

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

所以,你的最终网址是/ posts / destroySelected /?ids = 1(例如)

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

您的请求中的参数$ ids需要url route参数(例如route / posts / destroySelected / {ids})而不是查询参数(_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:

要获取查询参数(_GET),您可以使用:

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

#1


1  

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

你没有向控制器提供任何数据:)

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

你有:url:'/ posts / destroySelected /'和数据:{ids:ids}

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

所以,你的最终网址是/ posts / destroySelected /?ids = 1(例如)

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

您的请求中的参数$ ids需要url route参数(例如route / posts / destroySelected / {ids})而不是查询参数(_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:

要获取查询参数(_GET),您可以使用:

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