Ajax 成功后刷新/重新加载页面 - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47483380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh/Reload page after Ajax Sucess - Laravel
提问by pogba
In my laravel project, i want to refresh my page after an ajax success but my page would refresh after the success. I tried to refresh with laravel redirect in the controller and it didn't work, i have also tried to refresh in the ajax and nothing happened? How do i do this right? How do i do this right?
在我的 Laravel 项目中,我想在 ajax 成功后刷新我的页面,但我的页面会在成功后刷新。我尝试在控制器中使用 laravel 重定向进行刷新,但没有成功,我也尝试在 ajax 中刷新,但什么也没发生?我该怎么做?我该怎么做?
Controller
控制器
if(request()->ajax()) {
//do something
return ['success' => 'successfully done'];
return redirect('admin/all')->with('status','Successfully done!');
JS
JS
<script type="text/javascript">
$('#master').on('click', function(e) {
if($(this).is(':checked',true))
{
$(".sub_chk").prop('checked', true);
} else {
$(".sub_chk").prop('checked',false);
}
});
$('.approve_all').on('click', function(e) {
var allVals = [];
$(".sub_chk:checked").each(function() {
allVals.push($(this).attr('data-id'));
});
if(allVals.length <=0)
{
alert("Please select row.");
}
else {
var check = confirm("Are you sure you want to delete this row?");
if(check == true){
var join_selected_values = allVals.join(",");
$.ajax({
url: $(this).data('url'),
type: 'GET',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: 'ids='+join_selected_values,
success: function (data) {
if (data['success'])
{
$("#" + data['tr']).slideUp("slow");
alert(data['success']);
location="/admin/all";
}
else if (data['error'])
{
alert(data['error']);
}
else
{
//alert('Whoops Something went wrong!!');
}
},
error: function (data) {
alert(data.responseText);
}
});
window.location.href="/your/url" ;
$.each(allVals, function( index, value )
{
$('table tr').filter("[data-row-id='" + value + "']").remove();
});
}
}
$('[data-toggle=confirmation]').confirmation({
rootSelector: '[data-toggle=confirmation]',
onConfirm: function (event, element) {
element.trigger('confirm');
}
});
$(document).on('confirm', function (e) {
var ele = e.target;
e.preventDefault();
$.ajax({
url: ele.href,
type: 'GET',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
success: function (data) {
if (data['success'])
{
$("#" + data['tr']).slideUp("slow");
alert(data['success']);
location="/admin/all";
}
else if (data['error']) {
alert(data['error']);
}
else
{
alert('Whoops Something went wrong!!');
}
},
error: function (data) {
alert(data.responseText);
}
});
return false;
});
});
</script>
采纳答案by Eddie
You need to use javascriptto refresh the page. You can use location.reload()
您需要使用javascript来刷新页面。您可以使用location.reload()
if ( data['success'] )
{
alert(data['success']);
location.reload();
}
回答by eliarms
For those of you still looking for a Solution, This is how you can resolve this issue.
对于那些仍在寻找解决方案的人,这是解决此问题的方法。
First of all, just return a simple message in your controller.
首先,只需在您的控制器中返回一条简单的消息。
class SettingsController extends Controller
{
public function __construct()
{
}
public function destroy($id)
{
$user = User::findOrFail($id);
$user->delete();
return 'Record successfully deleted';
}
}
Secondly, Add this code in your javascript file to refresh the page.
其次,将此代码添加到您的 javascript 文件中以刷新页面。
setTimeout(function () { document.location.reload(true); }, 5000);
setTimeout(function () { document.location.reload(true); }, 5000);
<script type="text/javascript">
function deluser(id)
{
event.preventDefault();
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url:"{{ route('settings.destroy','')}}/"+parseInt(id),
method: 'delete',
data:{
id: id,
},
success: function(data)
{
$('#validation-message').append('<div class="alert dark alert-success alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span> </button><b>'+data+'</b></div>');
setTimeout(function () { document.location.reload(true); }, 5000);
},
error: function(xhr)
{
$('#validation-message').html('');
$.each(xhr.responseJSON.errors, function(key,value) {
$('#validation-message').append('<div class="alert dark alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span> </button><b>'+value+'</b></div>');
});
}
});
}
</script>
Finally in your HTML.
最后在你的 HTML 中。
<!DOCTYPE html>
<html>
<body>
<div class="col-xl-12 form-group" id="validation-message"></div>
<button onclick="deluser('ID_NUMBER_GOES_HERE')">Click me</button>
</body>
</html>
回答by Soubhagya Kumar Barik
You can try something like this
你可以试试这样的
if (data['success'])
{
$("#" + data['tr']).slideUp("slow");
location.reload();
}

