Javascript 将带有简单字符串的请求作为请求正文
当我从浏览器中执行以下代码时,服务器会给我400,并抱怨请求正文丢失。有人知道我如何传递一个简单字符串并将其作为请求主体发送吗Javascript 将带有简单字符串的请求作为请求正文,javascript,axios,Javascript,Axios,当我从浏览器中执行以下代码时,服务器会给我400,并抱怨请求正文丢失。有人知道我如何传递一个简单字符串并将其作为请求主体发送吗 let content = 'Hello world' axios.put(url, content).then(response => { resolve(response.data.content) }, response => { this.handleEditError(response) }) 如果我将内容包装在[]
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)
})
但是第一个不也应该工作吗?axios.put(url,{body},{headers:{}})
例如:
const body={title:“什么!”}
常量api={
apikey:“safhjsdflajksdfh”,
授权:“基本bwejdkfhasjk”
}
轴心https://api.xxx.net/xx,正文,{headers:api})
这对我有用(从node js repl调用的代码):
const axios=require(“axios”);
axios
.放(
"http://localhost:4000/api/token",
“我的代币”,
{标题:{“内容类型”:“文本/普通”}
)
.then(r=>console.log(r.status))
.catch(e=>console.log(e));
日志:200
这是我的请求处理程序(我正在使用restify):
功能手柄(req,res){
if(type of req.body==“string”&&req.body.length>3){
res.send(200);
}否则{
res.send(400);
}
}
内容类型标题在这里很重要。您是否尝试过以下方法:
axios.post('/save', { firstName: 'Marlon', lastName: 'Bernardes' })
.then(function(response){
console.log('saved successfully')
});
参考资料:这对我很有效
let content='Hello world';
静态apicall(内容){
返回轴({
url:`url`,
方法:“放”,
数据:内容
});
}
apicall()
。然后((响应)=>{
console.log(“success”,response.data)
}
.error(()=>console.log('error');
这对我很有用:
export function modalSave(name,id){
console.log('modalChanges action ' + name+id);
return {
type: 'EDIT',
payload: new Promise((resolve, reject) => {
const value = {
Name: name,
ID: id,
}
axios({
method: 'put',
url: 'http://localhost:53203/api/values',
data: value,
config: { headers: {'Content-Type': 'multipart/form-data' }}
})
.then(function (response) {
if (response.status === 200) {
console.log("Update Success");
resolve();
}
})
.catch(function (response) {
console.log(response);
resolve();
});
})
};
}
我在发送纯文本时遇到问题,发现我需要用双引号将正文的值括起来:
const request = axios.put(url, "\"" + values.guid + "\"", {
headers: {
"Accept": "application/json",
"Content-type": "application/json",
"Authorization": "Bearer " + sessionStorage.getItem('jwt')
}
})
我的webapi服务器方法签名如下:
public IActionResult UpdateModelGuid([FromRoute] string guid, [FromBody] string newGuid)
我通过覆盖默认内容类型解决了这个问题:
const config = { headers: {'Content-Type': 'application/json'} };
axios.put(url, content, config).then(response => {
...
});
根据m的经验,默认的内容类型是application/x-www-form-urlencoded,用于字符串,application/json用于对象(包括数组)。您的服务器可能需要json。只需在标题中输入“Content-Type”:“application/json”和正文中发送的数据
json.stringify(string)
另一个简单的解决方案是在给定代码中用大括号括住内容变量,如下所示:
let content = 'Hello world'
axios.put(url, {content}).then(response => {
resolve(response.data.content)
}, response => {
this.handleEditError(response)
})
警告:但这不会将其作为字符串发送;它会将其包装在json正文中,如下所示:{content:“Hello world”}两者都不起作用。第一个的语法无效,第二个作为:{“content”:“my content”}…传递,这是预期的,因为{content}是{“content”的缩写:{“my content”}.axios.put(url,{body},{headers:{}}),顺序很重要。我试过我的代码,它可以工作。你的身份验证东西,比如API_键,进入headers的对象。不,当主体是字符串时,这不起作用。你的主体是一个对象…试着只使用一个普通字符串。这是对我有效的唯一方法(我正在执行
post
请求),但是怎么做呢?这个双引号解决方案就像魔术一样。如果你仔细想想,服务器端需要一个字符串数据类型。如果这个字符串值是一个对象类型字段的值,那么你仍然会在json属性中用双引号括住字符串值。这是一种推理的方法。不知怎的,这就像一个符咒非常疯狂。谢谢!对于json请求,您必须提供有效的json,例如axios.put('/foo',{foo:bar})
这不是警告,只是错误。您在不理解的情况下使用ES6功能进行对象初始化。在ES6中,这是{content:content}的缩写您并没有发送原始负载,而是创建了一个标准json对象,该对象的“内容”属性填充了原始值。