Javascript axios 使用 json 数据发布请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42958431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
axios post request with json data
提问by h_h
I am using Axios JS library for sending post json request. but I am not receiving anything at the server. Here is my code
我正在使用 Axios JS 库来发送 post json 请求。但我没有在服务器上收到任何东西。这是我的代码
const dt = JSON.stringify({"data":{"value":"gdfg1df2g2121dgfdg"}});
const request = axios.post(url, {dt});
I need to send post raw body in json format.
我需要以json 格式发送原始正文。
回答by Santiago Benitez
By default axiosuses Json for posting data so you don't need to stringify your data. The problem could be that you're doing that. Could you try doing the post without it and check if it works? Also you don't need the curly braces to wrap your data unless that's the format of the object in your server. Otherwise could you give me information about how the body of the request looks like so I have more context? You can check that in chrome dev tools using the network tab
默认情况下,axios使用 Json 来发布数据,因此您无需对数据进行字符串化。问题可能是你正在这样做。您可以尝试在没有它的情况下进行发布并检查它是否有效吗?此外,除非这是服务器中对象的格式,否则您不需要花括号来包装数据。否则,您能否提供有关请求正文的信息,以便我了解更多上下文?您可以使用网络选项卡在 chrome 开发工具中检查
回答by Ken Labso
You don't need to stringify your payload. Axios will do it for you when it it send a request.
您不需要对有效负载进行字符串化。Axios 会在它发送请求时为你做这件事。
const dt = { data: { value: "gdfg1df2g2121dgfdg" }};
const request = axios.post(url, dt);
回答by Bruce Lee
Axios for post request with json as its body:
以 json 为主体的 post 请求的 Axios:
static async postService(path, data = {}) {
const requestUrl = HttpRequest._getRequestUrl(path);
try {
const ret = await axios.post(requestUrl, JSON.stringify(data));
console.log('Request result ', ret);
} catch (error) {
console.error(`Request error: ${error.message}`);
}
}

![Javascript [Vue 警告]:属性或方法未在实例上定义但在渲染期间被引用](/res/img/loading.gif)