Do you have enctype for form, do you have the CSRF token field.
Nov 11, 2023
8
Level 2
Laravel 10 | Why ajax [POST] throw an error with route [POST]?
I've seen the post below, but I'm still unsure why the POST method doesn't work. I could probably use GET but .. am I using POST the wrong way? (the same code worked fine in Joomla) I removed the data on the URL (test + data), but Laravel return the same 405 . Many thanks.
POST ERR: http://localhost/jxfileupload?task=file_upload&mp3Len=89&pid=1&app=stt 405 (Method Not Allowed)
AJAX :
var task = "?task=file_upload"; proid= proid?proid:1;
var data = "&mp3Len="+mp3Len+"&pid="+proid+"&app="+app;
var link = pagedata.app_url+"/jxfileupload/"+task+data; // localhost/jxfileupload +task+data
var file_data = $('#select_file').prop('files')[0];
var fdata = new FormData();
fdata.append('file', file_data);
$.ajax({
xhr: function() {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var percent = evt.loaded / evt.total;
percent = parseInt(percent * 100);
if (percent < 90) { $("#upload_prog").width(percent+"%").html(''); } // allow full upload
if (percent === 100) {
$("#upload_file").html("Uploaded").prop('disabled', true);
}
}
}, false);
return xhr;
},
url: link,
type: "POST",
data: (fdata),
processData: false,
contentType: false,
cache: false
// ...
});
AjaxController:
class AjaxController extends Controller
{
public function file_upload() {
// ...
}
//...
}
Route
Route::controller(AjaxController::class)->group(function () {
Route::get('/jxuserfiles', 'get_userFiles');
Route::post('/jxfileupload', 'file_upload');
});
Please or to participate in this conversation.