釘釘小程序post提交json,報400、415


下午又可以愉快的寫代碼了。被這個小問題折磨兩天了。其實原因很簡單就是提交的json格式不正確。

貼出來正確的代碼:

let domain ="http://cquni.vaiwan.com:8771/api/ECard";
let api_url=domain+'/workscan'; 
let app=getApp(); 
Page({
  data:{
    src:''
  },
  onLoad(){ 
      dd.scan({
        type: 'qr',
        success: (res) => { 
          const code_obj=res.code;
          if (code_obj.indexOf("{")==-1){
             dd.alert({content:'二維碼格式不符!'+'\r\n'+'['+code_obj+']'});
             return;
          }    
          //console.log(api_url+'?userId='+app.globalData.current_user);   
          //dd.alert({content:'二維碼的內容:'+'\r\n'+'['+code_obj+']'}); 
          const header = {'Content-Type': 'application/json'};   
          let json_data =JSON.stringify({'userId':app.globalData.current_user,'barcode':code_obj});   
          dd.httpRequest({
              url: api_url,
              method: 'POST',
              data:json_data,
              headers: header,
              dataType: 'json',
              success: function(info) {                      
                dd.alert({content:JSON.stringify(info)}); 
              },
              fail: function(res) {
                  dd.alert({content:'網絡故障!'+'\r\n'+JSON.stringify(res)});
              } 
            }); 
        },
      });
  }, 
})

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM