Laravel AJAX PUT&删除 [英] Laravel AJAX PUT & DELETE

查看:77
本文介绍了Laravel AJAX PUT&删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的开发机上,关于POST,PUT,DELETE和GET的一切工作正常.

On my development machine, everything works fine in regards to POST, PUT, DELETE, GET.

例如:

POST https://example.com/laravel/project (will create a new project - with data coming in from ajax)
PUT https://example.com/laravel/project/1 (will update the content of project with ID 1)
DELETE https://example.com/laravel/project/1 (will delete the project with ID 1)

但是,我现在将项目移至生产环境(另一台服务器)

However, I moved my project to production (a different server) and now

POST https://example.com/laravel/project (will create a new project as expected)
PUT https://example.com/laravel/project/1 (will not **update** project 1)
DELETE https://example.com/laravel/project/1 (will **not** delete project 1)

我检查了chrome的网络标签,可以看到存在的cookie和来自ajax调用的数据(例如,已更新/修改的字段).

I have checked chrome's network tab, and I can see the cookies being present and the data that is coming in from the ajax call (for example, the fields that are updated/modified).

此外,我的状态为200,所以从我的理解来看,网络服务器上也没有任何问题.

Also, I am getting a status 200 so there aren't any issues on webserver from what I understand too.

下面我的ajax调用示例-它们位于$ .ajax中,并且具有成功和失败功能.只显示重要的部分:)

Examples of my ajax calls below - they are in $.ajax and have success and fail functions. just showing the important bits :)

type: 'POST',
url: '/laravel/project',
data: {
    '_token': $('input[name=_token]').val(),
    'project_name': $('#project_name_add').val(),
    'category': $('#category_add').val()
}

type: 'PUT',
url: '/laravel/project/' + id,
data: {
    '_token': $('input[name=_token]').val(),
    'project_name': $('#project_name_edit').val(),
    'category': $('#category_edit').val()
},

但是,它实际上并没有更新或删除任何内容.

However, it is not actually updating or deleting anything.

感谢您的帮助.

推荐答案

尝试将隐藏字段添加到名为 _method 的表单中.该功能称为方法欺骗. https://laravel.com/docs/5.5/routing#form-method-欺骗

Try adding a hidden field to the form named _method. The feature is called method spoofing. https://laravel.com/docs/5.5/routing#form-method-spoofing

<form action="/foo/bar" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

这篇关于Laravel AJAX PUT&amp;删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆