uniAPP跨域問題及介面傳參數據項帶雙引號「」問題記錄

  • 2020 年 1 月 20 日
  • 筆記

uniapp開發H5時,基本上都會遇到跨域問題:

下面是個完整的請求示例:

關鍵在於header請求頭:

header: { 'Access-Control-Allow-Origin': '*', //跨域加上頭 'Content-Type': 'application/x-www-form-urlencoded' },

Content-Type傳入內容格式設置為'application/x-www-form-urlencoded'時

數據格式為"data":"66666"

與'Content-Type': 'application/json'

不同的是json格式不帶「」—>data:6666,

發送內容格式的不同:數據格式也就不同;

具體內容類型參照:http://tool.oschina.net/commons

//登錄請求開始                      let loginurl = '/betago-api/auth/login';                      let isok=false;                      var that=this;                      // console.log(datas);                      uni.request({                          url: loginurl, //登錄API地址。                          data: {                              mobile: datas.account,                              password: datas.password                          },                          method: 'POST',                          dataType: "jsonp",                          async: true,                          header: {                              'Access-Control-Allow-Origin': '*', //跨域加上頭                              'Content-Type': 'application/x-www-form-urlencoded'                          },                          //登錄成功後返回                          success: function(res) {                              console.log(res.data);                              //登錄成功後存入快取用戶資訊(必要資訊)                              let dataInfo=JSON.parse(res.data);                              if (dataInfo.code=="0") {                                const userInfo1 = {                                  account: datas.account,                                  password: datas.password,                                  headImagePath: dataInfo.data.headImagePath,                                  coins: dataInfo.data.coins,                                  signature: dataInfo.data.signature,                                  userId: dataInfo.data.userId,                                  userTypeCode: dataInfo.data.userTypeCode,                                  testPlannedDate: dataInfo.data.testPlannedDate,                                  fileOssUpload: dataInfo.data.fileOssUpload,                                  LoginName: dataInfo.data.name,                              };                              service.addUser(userInfo1);                              //返回登錄成功資訊                              isok= true;                                }                              if (isok) {                                  that.toMain(datas.account);                              } else {                                  uni.showToast({                                      icon: 'none',                                      title: '用戶帳號或密碼不正確',                                  });                              }                          }                      });

補充知識:

租用主機

按需購買:根據云主機記憶體大小,硬碟容量,伺服器頻寬、作業系統不同所售價格也不同 ①:騰訊雲代金券2860禮包(希望對你有所幫助) ②:目前騰訊雲3折秒殺活動,價格非常實惠。