当我从我的浏览器执行下面的代码时,服务器给了我400,并抱怨请求体缺失。任何人都知道我如何传递一个简单的字符串并让它作为请求体发送?把简单的字符串作为请求主体的请求
let content = 'Hello world'
axios.put(url, content).then(response => {
resolve(response.data.content)
}, response => {
this.handleEditError(response)
})
如果我在[]中包装内容,它会通过。但是,然后服务器将它作为一个以[开头并且以]结尾的字符串来接收。这看起来很奇怪。
摸索之后,我发现了以下工作
let req = {
url,
method: 'PUT',
data: content
}
axios(req).then(response => {
resolve(response.data.content)
}, response => {
this.handleEditError(response)
})
而是应该不是第一个工作呢?
两者都不起作用。第一个语法无效,第二个语法为:{“content”:“我的内容”} ...这是{content}是{“content”:“我的内容”}的简称。 – user672009
axios.put(url,{body},{headers:{}}),顺序很重要。我尝试了我的代码,它的工作原理。您的身份验证内容(如API_KEY)会进入头文件的对象中。 –
不,这在身体是一个字符串时不起作用。你的身体是一个物体......用简单的字符串尝试。 – user672009