在我的laravel项目中,我希望在ajax成功后刷新我的页面,但是我的页面会在成功后刷新。我尝试用控制器中的laravel重定向刷新它并没有工作,我也尝试刷新ajax并且没有发生任何事情?我该怎么做?我该怎么做?
控制器
if(request()->ajax()) {
//do something
return ['success' => 'successfully done'];
return redirect('admin/all')->with('status','Successfully done!');
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>
答案 0 :(得分:2)
您需要使用javascript
刷新页面。您可以使用location.reload()
if ( data['success'] )
{
alert(data['success']);
location.reload();
}
答案 1 :(得分:1)
对于仍在寻找解决方案的您,这是解决此问题的方法。
首先,只需在控制器中返回一条简单消息即可。
class SettingsController extends Controller
{
public function __construct()
{
}
public function destroy($id)
{
$user = User::findOrFail($id);
$user->delete();
return 'Record successfully deleted';
}
}
第二,在您的javascript文件中添加此代码以刷新页面。
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>
最后在您的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>
答案 2 :(得分:0)
您可以尝试这样的事情
if (data['success'])
{
$("#" + data['tr']).slideUp("slow");
location.reload();
}