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