我在Laravel5.4中的DELETE Ajax请求中得到405(Method Not Allowed)错误

时间:2017-05-07 11:34:16

标签: javascript ajax laravel laravel-5 laravel-4

我是Laravel的新手。

我想通过ajax调用从数据库表中删除记录,但我在下面收到此错误。

  

DELETE http://192.168.33.10/staff/4/edit 405(方法不允许)   onDeleteButtonClick @ d2d.js:75

我向" http://192.168.33.10/staff/4"发送ajax请求在我的JavaScript代码中,但似乎请求已发送到" http://192.168.33.10/staff/4/edit"。

如何向" http://192.168.33.10/staff/4"?

提出我的ajax请求?

http://192.168.33.10/staff/4/edit

上的HTML表单
<form method="post" action="http://192.168.33.10/staff/{{  $staff->id  }}">

    <div class="form-group row">
        <div class="col-6">
            <label for="">last name</label>
            <input type="text" class="form-control" id="" name="last_name" placeholder="" value="{{  $staff->last_name }}">
        </div>

        <div class="col-6">
            <label for="">first name</label>
            <input type="text" class="form-control" id="" name="first_name" placeholder="" value="{{  $staff->first_name }}">
        </div>

    </div>

    <button type="submit" class="btn btn-primary">update</button>
    {{ method_field('PUT') }}


    <button id="delete-button" type="button" class="btn btn-danger">delete</button>

    <input id="hidden-value" type="hidden" name="id" value="{{  $staff->id  }}"/>
    {{ csrf_field() }}


</form>

的JavaScript

var $deleteButton = document.getElementById("delete-button");

$deleteButton.addEventListener("click", onDeleteButtonClick);

function onDeleteButtonClick(event) {


    var request = new XMLHttpRequest();

    request.ontimeout = function(event){
        console.log("timeout error");
    };

    request.onreadystatechange = function() {


        if(request.readyState === 4) {

            console.log("done");

            if(request.state === 0) {

                console.log("fail");

            } else {

                if(request.status === 200) {

                    console.log("request success");
                    location.href = "http://192.168.33.10/staff";

                } else {

                    console.log("request fail");



                }

            }

        }

    };

    request.onload = function(event) {
        console.log("onload");
        console.log(request.response);

    };

    request.onerror = function(event) {
        console.log("onerror");
        console.log(request.response);

        console.log(request.responseText);

    };

    var id = document.getElementsByName("id")[0].value;
    var token = document.getElementsByName("_token")[0].value;

    var url = "http://192.168.33.10/staff/" + id;


    request.open("DELETE", url, true);

    request.responseType = "json";
    request.timeout = 2000;

    request.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');

    request.send('id=' + encodeURIComponent(id) + '&' + '_token=' + encodeURIComponent(token));

}

标题信息

GENERAL:

Request URL:http://192.168.33.10/staff/4/edit
Request Method:DELETE
Status Code:405 Method Not Allowed
Remote Address:192.168.33.10:80
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source

RESPONSE HEADERS:

allow:GET, HEAD
Cache-Control:no-cache, private
Connection:close
Content-Type:text/html; charset=UTF-8
Date:Sun, 07 May 2017 11:17:09 GMT
Server:Apache/2.4.6 (CentOS) PHP/7.0.18
X-Powered-By:PHP/7.0.18
Request Headers
view source

REQUEST HEADERS:

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:ja,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:52
content-type:application/x-www-form-urlencoded;charset=UTF-8
Cookie:XSRF-TOKEN=eyJpdiI6ImViWGdkU3RCdkxORk9aV3RpMnNmTkE9PSIsInZhbHVlIjoidGRaT1wvdEpadlVFV1djcE05anhraktKY041ZmZ5VWpISGpjTzBmVk1HXC80ODNjOGNtY05Hc3RDSWw4dHM1M2h4eVRkMEJqVnUzR3FjMzhTMzE2YTlGZz09IiwibWFjIjoiYTNlYjc4MGNhYzkyOWQxMzhkMWNjYWNlNjIyNzNiOGU5M2M4OTE3ZjA0YzE1MmY3NjY4MjdkZTU0YWY5OTE3NSJ9; laravel_session=eyJpdiI6IkpyaWRwVit2NE03NURRSG03NlhCU0E9PSIsInZhbHVlIjoiODBIMGVyYkV6bHhEQzlWZXZIVTRoVFwvT0RnNTJweXBqNG14dnBzYWN4bXFJT3lKcTdTb1FsMUJqejFucE5wdThKNkhQWkY2aDdZWFMwbUhLM2RxaE5nPT0iLCJtYWMiOiI0YTQyN2I1ZjA3NjFhNzY0NWM1ZDRlMzVlOTZiNTI2OTc5M2M2NmMzNmNkNDA2ZWY4Nzc0NjlmYzBmZTgzNGE1In0%3D
Host:192.168.33.10
Origin:http://192.168.33.10
Referer:http://192.168.33.10/staff/4/edit
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36
Form Data
view source
view URL encoded
id:4
_token:zQEHxnSdNlIB63wQ4lzxbHYO6wq5YMEhqddky46L

路线定义

[vagrant@localhost door]$ php artisan route:list
+--------+-----------+------------------------------+----------------------+---------------------------------------------------------------+--------------+
| Domain | Method    | URI                          | Name                 | Action                                                        | Middleware   |
+--------+-----------+------------------------------+----------------------+---------------------------------------------------------------+--------------+
|        | GET|HEAD  | /                            |                      | Closure                                                       | web          |
|        | GET|HEAD  | _debugbar/assets/javascript  | debugbar.assets.js   | Barryvdh\Debugbar\Controllers\AssetController@js              |              |
|        | GET|HEAD  | _debugbar/assets/stylesheets | debugbar.assets.css  | Barryvdh\Debugbar\Controllers\AssetController@css             |              |
|        | GET|HEAD  | _debugbar/clockwork/{id}     | debugbar.clockwork   | Barryvdh\Debugbar\Controllers\OpenHandlerController@clockwork |              |
|        | GET|HEAD  | _debugbar/open               | debugbar.openhandler | Barryvdh\Debugbar\Controllers\OpenHandlerController@handle    |              |
|        | GET|HEAD  | api/user                     |                      | Closure                                                       | api,auth:api |
|        | GET|HEAD  | staff                        | staff.index          | App\Http\Controllers\StaffController@index                    | web          |
|        | POST      | staff                        | staff.store          | App\Http\Controllers\StaffController@store                    | web          |
|        | GET|HEAD  | staff/create                 | staff.create         | App\Http\Controllers\StaffController@create                   | web          |
|        | GET|HEAD  | staff/{staff}                | staff.show           | App\Http\Controllers\StaffController@show                     | web          |
|        | PUT|PATCH | staff/{staff}                | staff.update         | App\Http\Controllers\StaffController@update                   | web          |
|  >>>   | DELETE    | staff/{staff}                | staff.destroy        | App\Http\Controllers\StaffController@destroy                  | web          |
|        | GET|HEAD  | staff/{staff}/edit           | staff.edit           | App\Http\Controllers\StaffController@edit                     | web          |
+--------+-----------+------------------------------+----------------------+---------------------------------------------------------------+--------------+

StaffController

class StaffController extends Controller
{

    //other methods here...

    //delete
    public function destroy($id, StaffRequest $request) {

        $staff = Staff::findOrFail($id);
        $staff->delete();

        return response()->json(['apple' => 'red', 'peach' => 'pink']);

    }


}

更新

request.setRequestHeader('XSRF-TOKEN', token);
//request.setRequestHeader('X-CSRF-TOKEN', token);
request.send('id=' + encodeURIComponent(id));
// request.send('id=' + encodeURIComponent(id) + '&' + '_token=' + encodeURIComponent(token));

我在我的JavaScript代码中更改了两行,如上所述,现在我向&#34; http://192.168.33.10/staff/4&#34;发送DELETE请求。

但是我得到500错误而不是405错误。

1 个答案:

答案 0 :(得分:1)

将链接DELETE http://192.168.33.10/staff/4/edit中的修改参数移除为DELETE http://192.168.33.10/staff/4