【发布时间】:2021-04-04 06:06:10
【问题描述】:
我正在尝试使用 ajax 向控制器发送数据,但我得到 此路由不支持 POST 方法。支持的方法:GET、HEAD。 谁能告诉我我做错了什么?
在 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>
路线列表:
!!!更新!!!
【问题讨论】:
-
检查浏览器开发工具中的网络选项卡以查看通过 ajax 发出的请求。
-
你是这个意思吗?请求 URL:localhost/MatchmakingForms/public/… 请求方法:POST 状态代码:405 方法不允许远程地址:[::1]:80 推荐人策略:strict-origin-when-cross-origin
-
如果您在 ajax 请求之前执行
console.log(route),它会记录什么 -
它不记录任何东西。
-
@hcphoon 正在做某事,我认为您没有在
/fb上提出请求,这就是不支持请求方法的原因。换句话说,route变量似乎是问题所在。但要确保,您应该检查它是否返回预期值。
标签: javascript jquery ajax laravel post