uniapp的uni.request请求在浏览器(H5)可以发送请求,真机调试发送请求无效,后端接受不到参数,已解决!

首先,我们看下请求,如果在真机上调试的话, 建议和我这样写,可以通过msg来看出是啥问题

<template>
	<view>
		<button @click="getMsg">按钮</button>
		<view>
			{{ msg }}
		</view>
	
	</view>
</template>

<script>
export default {
	data() {
		return {
			msg: '123'
		}
	},
	methods: {
		getMsg() {
			uni.request({
				header: {
					'content-type': 'application/json;charset:utf-8'    //重点
				},
				url: 'http://localhost:8080/api/user/login', //注意这个api
				
				method: 'POST',
				data: JSON.stringify({username: "001", password: "123"}),
				dataType: 'json',
				success: (res) => {
					console.log(res)
					this.msg = res.data.code
					console.log(res.data.code)
					
				},
				fail: (err)  => {
					this.msg = err
				}
			});
		},
	}
}
</script>

<style lang="scss" scoped>


</style>

然后就发现了报错

这里我们发下,其实就是因为在H5(浏览器)端配置了跨域

module.exports = {
  devServer: {
    proxy: {
      '/api': { //axios要请求的,name
        target: 'http://192.168.xxx.xxx:8080', //target为目标地址
        changeOrigin: true, //开启跨域
        pathRewrite: { //重写路径
          '^/api': '' //按照模板,name
        }
      }
    }
  }
}

 但是我们并没有配置app端的地址,也就是说,我们要把请求地址写全就可以了

 

<template>
	<view>
		<button @click="getMsg">按钮</button>
		<view>
			{{ msg }}
		</view>
	
	</view>
</template>

<script>
export default {
	data() {
		return {
			msg: '123'
		}
	},
	methods: {
		getMsg() {
			uni.request({
				header: {
					'content-type': 'application/json;charset:utf-8'    //重点
				},
				// url: 'http://localhost:8080/api/user/login', 
				url: 'http://198.168.xxx.xxx:8080/user/login', //把它给写全
				
				method: 'POST',
				data: JSON.stringify({username: "001", password: "123"}),
				dataType: 'json',
				success: (res) => {
					console.log(res)
					this.msg = res.data.code
					console.log(res.data.code)
					
				},
				fail: (err)  => {
					this.msg = err
				}
			});
		},
	}
}
</script>

<style lang="scss" scoped>


</style>

然后,咱们就能给他调通了,当然我们也可以在配置H5的同时给APP也配置了

具体操作,请看uni-app真机调试出现 request:fail abort statusCode:-1 Expected URL scheme ‘http‘ or ‘https‘ but was ‘file‘ - 拿我格子衫来的代码笔记如果对你有帮助的话,给博主点个赞吧

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值