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折秒杀活动,价格非常实惠。