[英]How to pass an array of object into a payload in a JSON format while making a api request
我有一个这样的对象数组。
var arr = [
{name: 'myName', age: 25, city: 'myCity'},
{name: 'myName1', age: 25, city: 'myCity1'}
];
现在,我需要将此 arr 数组作为 JSON object 传递到有效负载中。 我使用JSON.stringify(arr)
然后传递了数据。 但它正在转换为表单数据,有效负载格式如下所示;
[{name: 'myName', age: 25, city: 'myCity'},{name: 'myName1', age: 25, city: 'myCity1'}]:
如果您注意到有效负载末尾的冒号,似乎它将我的数组作为属性名称转换为表单数据。 你能帮我将数组作为 JSON object 传递吗?
用于调用 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
}
dataType: 'json'
contentType: 'application/json'
因为默认是'application/x-www-form-urlencoded; charset=UTF-8'
'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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.