axios 发送post请求


方案一

在node中使用axios以post的方式发送一张图片给某个server时:

let data = fs.createReadStream(__dirname + '/test.jpg')
		axios.post(url,{media:data,type:"image"})
		.then(function (response) {
		console.log(response.data);
		})
		.catch(function (error) {
		console.log(error);
		})

方案二

事实证明,这样做是完全没有用的,我尝试向另一个服务器poststream,返回的总是错误。然而,如果我使用request,下面这样的代码是完全没有问题的:

let data = fs.createReadStream(__dirname + '/test.jpg')
let form = {
type:"image",
media:data
}
request.post({url:url,formData:form},(err,res,body)=>{
if(err) console.log(err)
console.log(body)
})
// 注册事件方法
    register: function() {
      let registerUrl = this.GetServerUrl() + "/user/signup";
      let params = {
        username: this.username,
        password: this.password,
        email:    this.email,
        captcha:  this.captcha
      };
      this.axios
        .post(registerUrl, params)
        .then(response => {
          if(response.status == 0) {
            
          }
        });
    }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM