Javascript 如何在多部分超级代理请求中发送对象和附加文件?
我正在尝试使用superagent向我的API发出多部分POST请求 我的代码:Javascript 如何在多部分超级代理请求中发送对象和附加文件?,javascript,ajax,node.js,superagent,Javascript,Ajax,Node.js,Superagent,我正在尝试使用superagent向我的API发出多部分POST请求 我的代码: superagent .post(apiUrl + '/api/company/profile/edit') .field("profileData", profileData) .attach('company_logo', logoFile ) .set('Accept', 'application/json') .end(function(err, res){ if(err){
superagent
.post(apiUrl + '/api/company/profile/edit')
.field("profileData", profileData)
.attach('company_logo', logoFile )
.set('Accept', 'application/json')
.end(function(err, res){
if(err){
dispatch(updateProfileStatusAction("error", res));
} else {
dispatch(updateProfileStatusAction("success", res));
}
});
我遇到的问题是,profileData
是一个嵌套的对象。当我在API中得到请求时,我看到profileData
的值是字符串[Object,Object]
当我使用superagent查看有关多部分请求的文档时,它看起来像
.field()
只是一个键、值对,而不是一个对象。然后我尝试使用.send({profileData:profileData})代替field,但当我这样做时,我得到一个错误,即.attach和.send不能在同一个请求中一起使用。我认为使用JSON.stringify()
将JS\u对象转换为JSON字符串应该足够了
superagent
.post(apiUrl + '/api/company/profile/edit')
.field("profileData", JSON.stringify(profileData))
.attach('company_logo', logoFile )
...