onchange JS函数的HTTP请求错误

HTTP Request Error With onchange JS Function

我正在尝试向 Laravel 内置的工单系统添加状态功能,如果工单处于打开、关闭、暂停等状态,该功能将保持不变。

我创建了状态 table,我可以在我的视图中引用 table。

视图有一个包含所有可能状态的下拉菜单,我想在从下拉菜单中选择新状态时更新工单的状态。

目前,我有一个 onchange JavaScript 函数 运行,但我在从 JavaScript 函数提交 POST 请求时遇到问题。重定向到 POST 路由时,我收到错误: MethodNotAllowedHttpException.

如果我让每个状态都有自己的表单按钮,post 路由可以正常工作。

我上次收到此错误是因为我忘记将 CSRF 令牌传递到表单中。但是,我不知道如何使用 JavaScript 函数执行此操作。

我确定有一种方法可以完成我想做的事情,但我不确定如何去做。

感谢您的帮助!


这是我的观点

<form name="stateForm" action="{{ url($claim->path() . '/state') }}">
    <select id="stateList" onchange="stateChange(this.form)">
            @foreach($states as $state)
                <option id="{{ url($claim->path() . '/state/' . $state->id) }}" value="{{ $state->id }}">{{ $state->name }}</option>
            @endforeach
    </select>
</form>

JavaScript呼吁变革

function stateChange(stateForm, URL)
{
    var selIndex = stateForm.stateList.selectedIndex;
    var URL = stateForm.stateList.options[selIndex].id;
    window.location.href = URL;
}

Laravel路线

Route::post('/claims/{claim}/state/{stateID}', 'ClaimController@updateState');

使用表单和按钮的工作代码

<div class="btn-group">
@foreach( $states as $state)
    <form method="post" action="{{ url($claim->path() . '/state/'. $state->id) }}">
        {{ csrf_field() }}
        <button>
            {{ $state->name }}
        </button>
    </form>
@endforeach
</div>
window.location.href = URL;

基本上是重定向的get请求

您需要将路由更改为 GET 请求

Route::get('/claims/{claim}/state/{stateID}', 'ClaimController@updateState');