【发布时间】:2018-04-21 02:20:31
【问题描述】:
我正在使用 JavaScript 研究 Ajax,并希望在 Laravel 5.4 中创建一个 Ajax 发布方法。这些是我的文件...
路线
Route::group(['prefix' => 'admin'],function(){
Route::post('/ccat','PagesContrpllerController@ccat')->name('ccat');
Route::resource('/products' , 'ProductController');
});
ProductCategoryController
public function ccat(Request $request){
return 'hello this is post method';
}
JavaScript
function sendfunc(name , level , parent){
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4 && xhr.status==200) {
console.log(this.responseText);
}
}
xhr.open("POST", "ccat", true);
xhr.setRequestHeader("Content-type", "application/x-www-formurlencoded");
xhr.send("fname=Henry&lname=Ford");
}
我希望控制台中出现'hello this is the post method',但它却返回:
POST http://localhost:8000/admin/product/ccat 404(未找到)
控制台中发生了什么?即使我将 URL 更改为:http://localhost:8000/admin/ccat` 它也会返回:
POST http://localhost:8000/admin/ccat 500(内部服务器错误)
感谢您的帮助并忽略错误的编码。 :)
【问题讨论】:
标签: javascript ajax laravel laravel-5.4