我試圖讓捲曲請求(通過終端我的Mac上工作)到jQuery的Ajax和我得到一個400錯誤的請求。如果您發現我的代碼存在任何問題,請告訴我。這有助於使用Oanda v20 API。經由終端如何轉換捲曲請求,Ajax和捲曲-d體
捲曲(工作):
body=$(cat << EOF
{
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
EOF
)
curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <AUTHENTICATION TOKEN>" \
-d "$body" \
"https://api-fxpractice.oanda.com/v3/accounts/<ACCOUNT>/orders"
jQuery的AJAX(不工作):
$.ajax({
type: "POST",
url: 'https://api-fxpractice.oanda.com/v3/accounts/' + accountNumber + '/orders',
data: {
"order": {
"units": "10000",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
},
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + auth)
}, success: function(data){
console.log(data);
}
});
顯然,auth
,accountNumber
等是這些代碼之外定義有效變量。
噢,那也是。 – SeanKendle
@SeanKendle檢查答案更新 – Dekel
是的,我只是假設很快被封在試圖發送一個AJAX請求跨域。 – SeanKendle