Как я могу преобразовать следующий PHP Curl Request в Javascript POST?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://accounts.google.com/o/oauth2/token");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/x-www-form-urlencoded']);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'code' => $code,
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code',
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close ($ch);
Я пробовал что-то вроде ниже. Но получаю 400 ошибочных запросов. Как я могу установить CURLOPT_RETURNTRANSFER здесь. Или я что-то не так делаю?
$.ajax({
type: 'POST',
url: "https://accounts.google.com/o/oauth2/token",
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
data: {
client_id: client_id,
client_secret: client_secret,
code: code,
redirect_uri: redirect_uri,
grant_type: grant_type,
},
success: function (data) {
$('#response').html(data);
},
error: function (e) {
$('#response').html(e.responseText);
}
});