Currently, I am diving into Ajax using JavaScript and aiming to implement an Ajax post method in Laravel 5.4. Below are the snippets from my various files...
Routes
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");
}
Upon running the script, I anticipate seeing 'hello this is the post method'
printed in the console but instead encounter:
POST http://localhost:8000/admin/product/ccat 404 (Not Found)
Confused by this unexpected result, I tried adjusting the URL to: http://localhost:8000/admin/ccat` which unfortunately led to:
POST http://localhost:8000/admin/ccat 500 (Internal Server Error)
Your insights on resolving this issue would be greatly appreciated! Thank you for your understanding amidst any coding mishaps. :)