发出AJAX发布请求以及将JSON数据发送到laravel控制器函数。正在发送的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发布请求后,警报消息中期望响应值为以JSON格式发送的详细数据,但显示为[object Object]。
答案 0 :(得分:1)
尝试在laravel控制器中使用 json_encode()方法将数据转换为json格式。
public function afterpayment(Request $request)
{
$data = array('status' => $request->deatails,'url' => '/dispdetails');
$response = json_encode($data);
return $response;
}
答案 1 :(得分:0)
发送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);
}