Ajax return PUT ... 405(不允许使用方法)

时间:2018-08-17 22:25:41

标签: ajax laravel

web.php: Route::put('contact', 'FormsController@send');

script.js:

    const sendForm = function () {
        action = 'contact';

        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                const getMessageSend = document.querySelector("#messageSend");
                getMessageSend.classList.add("message-send");
                getMessageSend.innerText = "Thank you for sending an email. You will receive an answer shortly.";
            } else {
                const getMessageSendError = document.querySelector("#messageSendError");
                getMessageSendError.classList.add("message-send");
                getMessageSendError.classList.add("message-send-error");
                getMessageSendError.innerText = "An error occurred and the email was not sent.";
            }
        }
        xmlhttp.open("PUT", action, true);
        xmlhttp.send();

    };

和我的表格:

{!! Form::open(['action'=>['FormsController@send'], 'method' => 'PUT', 'class' => 'form', 'novalidate' => 'novalidate', 'files' => true]) !!}

    ...

{!! Form::submit('Send', ['class' => 'submit-btn']); !!}
{!! Form::close() !!}

Laravel翻倒错误:

script.js:566 PUT http://subdomain.mypage.com/contact 405 (Method Not Allowed)

我的名字是FormsController@send,名字地址是contact。我在索引文件的标题中添加了

但是错误仍然发生。我不知道我在做什么错。

编辑: 出现错误的更多信息:

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------15304174751887
Content-Length: 147
Cookie: XSRF-TOKEN=eyJpdiI6Imk0RVE5NDB3eHBXaGQ0OUwyc0VXa3c9PSIsInZhbHVlIjoiV2xPYzl4VzN1bEFJdDFBZ0JiZ1Nhd2JYZTVzRW1WekdobVwvNjNpY24wVmtEVUJQbFV1K1Q4anBydXdOSEt5WWIiLCJtYWMiOiI5MzgwYmMzNDU0ZTAzY2NkNzNkNGEwNjk0NTYxNDk2ZjE5NjQzMTVjNGViOTQ5ZTNhZmI5N2I3NDQzNjM2OTY2In0%3D; hr_session=eyJpdiI6IlVLVU9WN0pEZlVJbUZOeFdIRWZ0cnc9PSIsInZhbHVlIjoiVzFEenJyc2x6d09TQjV0bWczc05LMlwvNHZDZ3hKSDdaZEpaeEFnVXc2d0NmZVpTeGY1NUJocWFzOStDN3h6OWIiLCJtYWMiOiIwYTJjYjZhMjcyMzk3MWVlOGUzMDYxOTM4ODFiNTc1Y2QzMWQ4ZWRhY2Y1ZTdiMjIyYTg4MTFhM2I2NGE0OTI2In0%3D
Connection: keep-alive

1 个答案:

答案 0 :(得分:0)

尝试将xmlhttp.send();替换为

var formdata = new FormData();
formdata.append("_method", "put");
xmlhttp.send(formdata);