我正在尋找使用 jQuery 提供的 AJAX 在 javascript 中進行 API 呼叫,但我收到一個無法處理的物體錯誤(來自我的 fastapi 服務器的 pydantic 錯誤回應)。奇怪的是 curl 命令確實有效。我不清楚為什么我的服務器可以區分錯誤的 ajax 呼叫和成功的 curl 呼叫。
curl -X 'POST' \
'http://127.0.0.1:8010/api/update' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"gsid":"634ad79ee29c42396b0d4055","ticker":"SPX230317C04200000","security_type":5,"security_subtype":2005,"option_flavor":2,"underlying":{"gsid":"634ad6d1d89536dac325f871","ticker":"SPX"},"denominated_ccy":{"gsid":"634ad6d1d89536dac325f86e","ticker":"USD"},"expiry_date":"2023-03-17","strike":4200,"option_exercise":1,"expiry_series_type":20,"expiry_time_of_day":1,"settlement_type":1,"primary_exchange":"CBO","multiplier":100,"issuer":0,"description":0,"website":0,"as_of_date":"1970-01-01T00:00-05:00","expiry_datetime":"1969-12-31T19:00-05:00","identifiers":[{"id_type":2,"value":""},{"id_type":3,"value":""},{"id_type":4,"value":""},{"id_type":5,"value":""}]}'
我的 API 正確回應了此呼叫,并帶有以下 200 成功回應:
{
"success": true,
"created_security": false,
"gsid": "634ad79ee29c42396b0d4055",
"available_versions": [
"1970-01-01T00:00:00-05:00"
],
"message": "success"
}
使用 jQuery 進行 AJAX 呼叫
data = {"gsid":"634ad79ee29c42396b0d4055","ticker":"SPX230317C04200000","security_type":5,"security_subtype":2005,"option_flavor":2,"underlying":{"gsid":"634ad6d1d89536dac325f871","ticker":"SPX"},"denominated_ccy":{"gsid":"634ad6d1d89536dac325f86e","ticker":"USD"},"expiry_date":"2023-03-17","strike":4200,"option_exercise":1,"expiry_series_type":20,"expiry_time_of_day":1,"settlement_type":1,"primary_exchange":"CBO","multiplier":100,"issuer":0,"description":0,"website":0,"as_of_date":"1970-01-01T00:00-05:00","expiry_datetime":"1969-12-31T19:00-05:00","identifiers":[{"id_type":2,"value":""},{"id_type":3,"value":""},{"id_type":4,"value":""},{"id_type":5,"value":""}]};
payload = JSON.stringify(data);
$.ajax({
url: 'http://127.0.0.1:8010/api/update',
type : "POST",
dataType: 'json',
processData: false,
success: function(data){
console.log('success: ' JSON.stringify(data));
},
error: function(data){
console.log('error: ' JSON.stringify(data));
},
data : payload,
});
在這里,我從服務器收到以下 422 無法處理的物體回應:
{"readyState":4,"responseText":"{\"status_code\":10422,\"message\":\"4 validation errors for Request body value is not a valid dict (type=type_error.dict) body value is not a valid dict (type=type_error.dict) body value is not a valid dict (type=type_error.dict) body value is not a valid dict (type=type_error.dict)\",\"data\":null}","responseJSON":{"status_code":10422,"message":"4 validation errors for Request body value is not a valid dict (type=type_error.dict) body value is not a valid dict (type=type_error.dict) body value is not a valid dict (type=type_error.dict) body value is not a valid dict (type=type_error.dict)","data":null},"status":422,"statusText":"Unprocessable Entity"}
uj5u.com熱心網友回復:
編輯將以下內容添加到 ajax 呼叫中有效:
contentType: "application/json"
@addjunior 提出的答案
uj5u.com熱心網友回復:
就我個人而言,我永遠不會將 jQuery 用于任何事情。
下面是它在 JavaScript 中的實作方式。
fetch('http://127.0.0.1:8010/api/update', {
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
'gsid': '634ad79ee29c42396b0d4055',
'ticker': 'SPX230317C04200000',
'security_type': 5,
'security_subtype': 2005,
'option_flavor': 2,
'underlying': {
'gsid': '634ad6d1d89536dac325f871',
'ticker': 'SPX'
},
'denominated_ccy': {
'gsid': '634ad6d1d89536dac325f86e',
'ticker': 'USD'
},
'expiry_date': '2023-03-17',
'strike': 4200,
'option_exercise': 1,
'expiry_series_type': 20,
'expiry_time_of_day': 1,
'settlement_type': 1,
'primary_exchange': 'CBO',
'multiplier': 100,
'issuer': 0,
'description': 0,
'website': 0,
'as_of_date': '1970-01-01T00:00-05:00',
'expiry_datetime': '1969-12-31T19:00-05:00',
'identifiers': [
{
'id_type': 2,
'value': ''
},
{
'id_type': 3,
'value': ''
},
{
'id_type': 4,
'value': ''
},
{
'id_type': 5,
'value': ''
}
]
})
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521093.html
下一篇:將URL卷曲到vb.net
