php - Json 无法解析
问题描述
我正在尝试使用 Guzzle laravel 发布一个对象,我使用表单提交从我的视图传递它但是当我这样做时它说 INVALID FORMAT Json 无法解析
我的观点
<form method="POST" action="comparePrices">
@csrf
<input type="text" name="datas" id="textsss"/>
<input type="submit" value="save"/>
</form>
但是当我通过邮递员发布它时它工作正常
我的控制器
public function comparePrices(Request $request)
{
$token = DB::table('a_p_is_tokens')->select('*')->limit(1)->orderBy('id', 'desc')->get()->pluck('token')[0];
$client = new Client();
try {
$res = $client->post('https://test.api.amadeus.com/v1/shopping/flight-offers/pricing', [
'headers' => [
'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token,
],
'form_params' => [
"data"=>[
"type" => "flight-offers-pricing",
"flightOffers" => $request->datas,
],
// 'body' => array('data'=>$items),
]);
$res = json_decode($res->getBody()->getContents(), true);
$response = $res->getResponse();
print_r(json_decode($response->getBody(), true));
// return view('agents.agentsTickets');
} catch (RequestException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
// return [json_decode($request->datas)];
}
dd([$items]);
return view('agents.agentsTickets');
}
错误代码
{
"data": {
"errors": [
{
"code": 477,
"title": "INVALID FORMAT",
"detail": "JSON cannot be parsed",
"status": 400
}
]
}
}
解决方案
您的 js 代码执行http GET而不是http POST
window.location='postings/'+items;
如果应该可以将“项目”添加到 url abd perform http GET那么你可以尝试在 Postman 中做同样的事情 - 使用 GET 而不是 POST 并修改 url,而不是正文。
如果您打算在 js 中执行与 Postman 相同的操作,那么我建议您使用 window.location.href 从传递帖子数据中查看答案并修改您的 js 代码。或尝试https://reqbin.com/req/javascript/uzf79ewc/javascript-post-request-example