Javascript JS:我能把字典传给xhr.send()吗?

Javascript JS:我能把字典传给xhr.send()吗?,javascript,xmlhttprequest,Javascript,Xmlhttprequest,有没有办法将字典传递给xhr.send()函数如下: xhr.send( { 'first_key': 'value', 'second_key': 'value', ... } ); xhr.send('{"hello":"world"}') 或者像这样: xhr.send( { 'first_key': 'value', 'second_key': 'value', ... } ); xhr.send('{"hello":"world"}') 看 send()

有没有办法将
字典
传递给
xhr.send()
函数如下:

xhr.send( {
  'first_key': 'value',
  'second_key': 'value',
  ...
} );
xhr.send('{"hello":"world"}')
或者像这样:

xhr.send( {
  'first_key': 'value',
  'second_key': 'value',
  ...
} );
xhr.send('{"hello":"world"}')

send()
的参数可以是:

  • 一种文档,在这种情况下,它在发送之前被序列化
  • BodyInit,根据Fetch规范,它可以是Blob、BufferSource、FormData、URLSearchParams、ReadableStream或USVString对象
不能传递普通对象,但可以传递字符串


确保使用
setRequestHeader
指定您的内容类型,如果应用程序/json是您正在发布一个json字符串。

您是否研究过使用
json.stringify
setRequestHeader
?不起作用:我设置了头并发送了
json.stringify(dict)
,在我的服务器上,我只有空的dict.@0x3E7-然后,假设您没有遇到飞行前请求问题,该问题将在浏览器的开发工具控制台上报告…您可能在尝试读取请求时在服务器上出错。
print(request.POST)
outputs
我不知道你在使用什么框架。我已经找到了问题所在:JSON字符串在
请求.正文中。