在控制器函数中不能访问发出AJAX post请求并将JSON数据发送到laravel控制器function.The的JSON数据。
所发送的JSON数据必须可以在控制器内访问
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url : '/admin/maintenances/afterpayment',
method : 'POST',
dataType: "json",
data : details,
contentType : "application/json"
}).done(function(response){
alert('success '+JSON.stringify(response));
window.location = "dispdetails";
}).fail(function(jqXHR, textStatus, errorThrown){
alert('FAILED! ERROR: ' + errorThrown);
});
}); public function afterpayment(Request $request)
{ $response = array('status' => $request->deatails,'url' => '/dispdetails');
return response( )->json($response);
}成功执行AJAX post请求后,警报消息中的响应值应该是以JSON格式发送的详细数据,但它显示为对象对象。
发布于 2019-09-20 22:42:46
发送ajax请求
// Say it's your request payload
let details = { name: 'John Doe', city: 'Mumbai', status: 'Payment done'};
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
'url': '/admin/maintenances/afterpayment',
'type': 'POST',
'dataType': 'json',
'data': details,
}).done(function (response) {
alert('success: ' + JSON.stringify(response));
// Redirect to response url
window.location.replace(response.url);
}).fail(function(xhr, ajaxOps, error) {
console.log('Failed: ' + error);
});在你的laravel控制器中
public function afterpayment(Request $request)
{
// Assuming your returning entire request payload
return response()->json(['status' => $request->all(), 'url' => '/your-redirect-url'], 200);
}https://stackoverflow.com/questions/57863451
复制相似问题