在使用 laravel 5.0 和 ajax 时遇到 ajax 404 错误,我不明白为什么。
我的路线
Route::post('user/saved/ [
'as' => 'delete-topics',
'uses' => 'TopicController@deleteTopic',
]);
我的表单
<form action="{{action('TopicController@deleteTopic')}}" method="post" class="deleteform">
<input type="hidden" name="topic_id" value="{{$topic->topic_id}}">
<input type="submit" value="delete">
</form>
我的 ajax 调用
// process the form
$('.deleteform').submit(function(event) {
event.preventDefault();
var $form = $(this);
var formData = {
topic_id : $form.find('input[name=topic_id]').val()
};
// process the form
$.ajax({
type : 'POST',
url : 'saved',
data : formData,
dataType : 'json',
encode : true
})
我的 Controller
public function deleteTopic()
{
$topic_id = Request::get('topic_id');
if(Auth::check())
{
$user_id = Auth::user()->id;
$delete_topic = DB::table('topic_save')->where('user_id', $user_id)->where('topic_id', $topic_id)->delete();
}
$data['success'] = true;
$data['message'] = 'success';
echo json_encode($data);
}
我尝试将 ajax 调用中的 url 更改为“user/saved”以匹配路由,但仍然收到相同的 404 错误
谢谢大家。
最佳答案
将 URL 更改为您为表单设置的操作。那将是:
url = $form.attr('action');
$.ajax({
type : 'POST',
url : url,
data : formData,
dataType: 'json',
encode : true
})
关于php - Laravel ajax 404 未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38476348/