Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将post中的json发送到服务器节点_Javascript_Node.js_Rest_Axios - Fatal编程技术网

Javascript 将post中的json发送到服务器节点

Javascript 将post中的json发送到服务器节点,javascript,node.js,rest,axios,Javascript,Node.js,Rest,Axios,这是我使用axios在post中发送就绪json的代码 const axios=require(“axios”); 常数jsonObj={ 序列号:“090982037439”,//字符串 传感器类型:'ciccio bello',//字符串 注册的价值单位:{ 值:30,//valore 度量单位:“摄氏度”///字符串 }, 值注册地址:'29-04-2021'///string }; const json=json.stringify(jsonObj); axios({ 方法:“post”

这是我使用axios在post中发送就绪json的代码

const axios=require(“axios”);
常数jsonObj={
序列号:“090982037439”,//字符串
传感器类型:'ciccio bello',//字符串
注册的价值单位:{
值:30,//valore
度量单位:“摄氏度”///字符串
},
值注册地址:'29-04-2021'///string
};
const json=json.stringify(jsonObj);
axios({
方法:“post”,
网址:'http://localhost:3000/api/post',
数据:json
});
但是我总是得到下面的错误(415)

  • axios不需要对数据进行字符串化(或者这是可选的)
  • 尝试设置MIME类型
  • 415响应代码表示axios至少正在发送数据。所以检查后端的问题
  • 响应代码为非20X时的捕捉错误
  • 
    常量选项={
    标题:{'content-type':'application/json'}
    };
    轴心柱(
    “/login”,
    {
    名字:“芬恩”,
    姓:“威廉姆斯”
    },
    选择权
    ).catch((错误)=>{
    console.log(错误)
    });
    
    415表示不支持的媒体类型

    确保API端点支持json请求。祝你好运

    您是否尝试过(假设这是用于web开发)在chrome开发工具中打开网络选项卡?
    UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise 
    rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
    (node:11188) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.