I’m trying to send data to controller using ajax and I’m getting The POST method is not supported for this route. Supported methods: GET, HEAD.
Can someone please tell me what am I doing wrong?
In web.php
Route::post('/fb', 'FormController@fb')->name('fb');
HTML:
<form method="POST" id="formfb" enctype="multipart/form-data" data-route="{{ route('fb') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button id="fbbutton" type="submit" class="dropdown-item">Save</button>
</form>
JavaScript:
<script language="javascript">
$('#formfb').on('submit', function(e){
e.preventDefault();
var w= <?php echo $resolution['width'] ;?>;
var h= <?php echo $resolution['height'] ;?>;
$("#picture").show();
html2canvas($('#picture'), {
width: w,
height: h
}).then(function(canvas) {
var inputURI = canvas.toDataURL('image/png');
var binaryVal;
var inputMIME = inputURI.split(',')[0].split(':')[1].split(';')[0];
if (inputURI.split(',')[0].indexOf('base64') >= 0)
binaryVal = atob(inputURI.split(',')[1]);
else
binaryVal = unescape(inputURI.split(',')[1]);
var blobArray = [];
for (var index = 0; index < binaryVal.length; index++) {
blobArray.push(binaryVal.charCodeAt(index));
}
var blobObject = new Blob([blobArray], {
type: inputMIME
});
var formData = new FormData();
formData.append("blob", blobObject, "blob");
var route= $('#form-data').data();
var form_data= $(this);
$.ajax({
type: 'POST',
url: route,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
data: form_data.serialize(),
success: function(Response){
console.log(Response);
}
});
})
</script>
Routes list:
!!!UPDATE!!!
2
Answers
If you want to make your
url
parameter in the AJAX request dynamic, you can simply dodoing
{!! <PHP Code> !!}
allows you to pass PHP into your JSThe following code is now working after some changes on Apache’s and PHP’s configurations:
JavaScript / jQuery
PHP