[英]Laravel AJAX response loads wrong route/window
I have two routes:我有两条路线:
Route::get('/download/{hash}','DownloadController@exists')->name('exists');
Route::post('/download/{hash}','DownloadController@verify')->name('verify');
Procedure:程序:
Basically the user enters a URL
eg localhost/download/56iq45agosasa
the first route is called and the download.blade.php
view is shown.基本上,用户输入
URL
例如localhost/download/56iq45agosasa
第一个路由,并显示download.blade.php
视图。 There is a form, where the user has to input a password and when the submit button is clicked, an ajax
request is sent to the second (post) route.有一个表单,用户必须输入密码,当点击提交按钮时,
ajax
请求被发送到第二个(post)路由。
The DownloadController@verify
returns the json but displays it in a blank window (see screenshot) But it should be returned in the download.blade.php
view. DownloadController@verify
返回 json 但将其显示为空白 window(见屏幕截图)但它应该在download.blade.php
视图中返回。
Somehow the form disappears and the ajax success function is not called.不知何故,表格消失了,并且没有调用 ajax 成功 function 。
Code snippets Download.blade.php:代码片段下载.blade.php:
@section('content')
<div class="container-fluid mt-5">
<div class="row justify-content-md-center">
<div class="col-md-4">
<form method="POST">
<label for="uploadPassword">Password</label>
<input type="password" name="password" class="form-control" id="uploadPassword" placeholder="Password" required="">
<button id="btn-submit" type="submit" class="btn btn-success mt-2">Submit</button>
{{ csrf_field() }}
</form>
</div>
</div>
</div>
@endsection
@push('scripts')
<script type="text/javascript">
$(".btn-submit").click(function(e){
e.preventDefault();
let password = $("input[name=password]").val();
$.ajax({
type:'POST',
url:"",
data:{
password:password
},
success:function(data){
console.log("I don't get shown");
//alert(data.success + " " + data.matches);
}
});
});
</script>
@endpush
DownloadController:下载控制器:
class DownloadController extends Controller
{
public function exists(Request $request, $hash)
{
// Some private mysql queries
// Return the hash to the download view
return view('download',[
'hash' => $hash
]);
}
public function verify(Request $request, $hash)
{
return response()->json(
['success'=>'Got Simple Ajax Request.']
);
}
}
So there are two things that will help improve this code:因此,有两件事将有助于改进此代码:
document.ready
function to ensure it's attached when the button is available in the page.document.ready
function 中,以确保在页面中的按钮可用时附加它。 This is necessary if @push
will make the script end up above the form declaration in the page.@push
将使脚本在页面中的表单声明上方结束,这是必要的。@push('scripts')
<script type="text/javascript">
$(function () {
$("form").on('submit', function (e) {
e.preventDefault();
let password = $("input[name=password]").val();
$.ajax({
type:'POST',
url:"",
data:{
password:password
},
success:function(data){
console.log("I don't get shown");
//alert(data.success + " " + data.matches);
}
});
});
});
</script>
@endpush
The problem here is that the form gets submitted.这里的问题是表单被提交。 Instead of overriding button click with your jQuery function, use it to change submit handler like so
不要用您的 jQuery function 覆盖按钮单击,而是使用它来更改提交处理程序,如下所示
Prevent Default on Form Submit jQuery 防止表单提交时出现违约 jQuery
Make sure you put id on the form.确保您在表格上输入了 id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.