我构建了允许更新和上传帖子的应用程序。我通过ajax进行更新后,但出于某种原因,我收到“POST 405(方法不允许)”的错误。
我做了类似的代码来写一篇新帖子,它运行正常。您认为这是什么问题?
以下是代码:
$("form#updata_post").submit(function(event){
event.preventDefault();
tinyMCE.triggerSave();
text_body = $("#body").val();
$("#all_body").val(text_body);
var formData = new FormData($(this)[0]);
$.ajax({
url: "{{Request::root(); }}/updata_post/",
dataType: "json",
type: "POST",
data: formData,
success: function(data) {
$(".info").hide().find("ul").empty();
if (!data.success) {
$.each(data.errors, function(index, error){
$(".info").append("<ul>" + error + "</ul>");
});
$(".info").slideDown( "slow" );
}
else {
window.location = "{{URL::to('posts')}}";
}
},
cache: false,
contentType: false,
processData: false
});
});
public function updata_post() {
$validator = Validator::make(Input::all(), postmodel::$rules);
if ($validator->passes()) {
$id = Input::get('id');
$post = postmodel::find($id);
$img_file = Input::file('file');
if (!empty($img_file)) {
$image = Input::file('file');
$imagename = rand(1,99999).$image->getClientOriginalName();
$upload = $image->move(public_path() . "/img/",$imagename);
$post->main_img = $imagename;
}
$post->description = Input::get('description');
$post->title = Input::get('title');
$post->all_files = Input::get('all_files');
$slug = Input::get('slug');
if (!empty($slug)) {
$post->slug = str_replace(" ", "-", Input::get('slug'));
}
else {
$post->slug = str_replace(" ", "-", Input::get('title'));
}
$post->body = Input::get('text_body');
$post->name = Input::get('name');
$post->section = Input::get('section');
$post->save();
//return Redirect::to('posts');
return Response::json(['success' => true]);
}
else {
return Response::json(['success' => false, 'errors' => $validator->messages()->toArray()]);
}