uniapp开发H5时,基本上都会遇到跨域问题:json
下面是个完整的请求示例:api
关键在于header请求头:跨域
header: {
'Access-Control-Allow-Origin': '*', //跨域加上头
'Content-Type': 'application/x-www-form-urlencoded'
},缓存
Content-Type传入内容格式设置为'application/x-www-form-urlencoded'时app
数据格式为"data":"66666"async
与'Content-Type': 'application/json'jsonp
不一样的是json格式不带“”--->data:6666,this
发送内容格式的不一样:数据格式也就不一样;url
具体内容类型参照:http://tool.oschina.net/commons.net
//登陆请求开始 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: '用户帐号或密码不正确', }); } } });