我有一個這樣的物件陣列。
var arr = [
{name: 'myName', age: 25, city: 'myCity'},
{name: 'myName1', age: 25, city: 'myCity1'}
];
現在,我需要將此 arr 陣列作為 JSON 物件傳遞到有效負載中。我使用JSON.stringify(arr)然后傳遞資料。但它正在轉換為表單資料,有效負載格式如下所示;
[{name: 'myName', age: 25, city: 'myCity'},{name: 'myName1', age: 25, city: 'myCity1'}]:
如果您注意到有效負載末尾的冒號,似乎它將我的陣列作為屬性名稱轉換為表單資料。你能幫我將陣列作為 JSON 物件傳遞嗎?
用于呼叫 API 方法的代碼是,
{
type: 'POST',
method: 'POST',
dataType: 'json',
url: 'url of the api call',
beforeSend: function(header, settingsData){
Object.keys(settingsData.options).forEach(headerKey){
headerKey.setHeader(headerKey, settingsData.options[headerKey]);
}
return settingsData;
},
disableDefaultError: true
}
uj5u.com熱心網友回復:
我的評論對了一半。
- 保持
dataType: 'json' - 添加
contentType: 'application/json',因為默認是'application/x-www-form-urlencoded; charset=UTF-8'
{
type: 'POST',
method: 'POST',
dataType: 'json',
contentType: 'application/json', // <- this is needed
url: 'url of the api call',
beforeSend: function(header, settingsData){
Object.keys(settingsData.options).forEach(headerKey){
headerKey.setHeader(headerKey, settingsData.options[headerKey]);
}
return settingsData;
},
disableDefaultError: true
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/416642.html
標籤:
