我如何在 React Native 中构造一个 POST 请求主体而不是字符串化的 json 而是 json?

How can I construct a POST request body in React Native not with a stringified json but a json?

我正致力于用 React Native 替换一些本机代码。 Charles中预期的POST请求(在AFNetworking中实现)应该是这样的:

代码片段:

NSError *err;
NSData *paramData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&err];
NSData *paramData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&err];   
NSString *paramString = [[NSString alloc] initWithData:paramData encoding:NSUTF8StringEncoding];
NSDictionary *param = @{@"data":paramString};
AFHTTPRequestOperation *operation = [manager POST:URLString parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
    if (successBlock) {
        successBlock(responseObject);
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    DebugLog(@"%zd", operation.response.statusCode);
    if (failureBlock) {
        failureBlock(operation, error);
    }
}];

但是Fetch API版本的请求是这样的:

代码片段:

export default async (url, param) => {
  var result = await fetch(url, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'text/html',
    },
    body: JSON.stringify({ 
      'data': JSON.stringify(param) 
    })
  })
  .then(response => checkStatus(response))
  .then(response => response.json())
  .catch(e => { throw e; });

  return result;
}

我的问题是如何在 fetch 中发送 post 与在 AFNetworking 中完全一样?这花费了我很多时间。谢谢!!

已更新: 主要区别在于烦人的斜杠及其正文数据结构,原生的是 json (data: paramString ), 而js字符串.

终于,同事找到了解决办法。请求体应该这样构造:

body: 'data=' + JSON.stringify(param)