uni-app 真机调试中 uni.request post请求被转为get请求

uni-app 真机调试中 uni.request post请求被转为get请求

操作步骤:

  • 直接开启调试插件可看

预期结果:

  • 真机 uni.request能正常请求

实际结果:

  • post请求被转为get

bug描述:

  • 内置浏览器开发一切正常
  • 真机调试post请求被转为get
  • 错误信息是我服务器返回
  • 图2是服务器返回的调试信息

图片

Image 1 Image 2

信息整理表

信息类别 详细信息
产品分类 uniapp/App
PC开发环境操作系统 Mac
PC开发环境操作系统版本号 10.14.6
HBuilderX类型 正式
HBuilderX版本号 3.2.14
手机系统 Android
手机系统版本号 Android 10
手机厂商 三星
手机机型 note9
页面类型 vue
vue版本 vue2
打包方式 云端
项目创建方式 HBuilderX

更多关于uni-app 真机调试中 uni.request post请求被转为get请求的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

看是不是跨域问题,

更多关于uni-app 真机调试中 uni.request post请求被转为get请求的实战教程也可以访问 https://www.itying.com/category-93-b0.html


问题解决了,mac上面有bug,win上面没问题

这个问题通常是由于跨域请求被服务器或中间件拦截导致的。从你提供的服务器返回信息来看,服务器明确提示"GET method not allowed",说明请求确实被转换为了GET方法。

主要原因和解决方案:

  1. 跨域预检请求(CORS Preflight)

    • 当uni-app向不同域名的服务器发送POST请求时,浏览器会先发送一个OPTIONS方法的预检请求
    • 如果服务器没有正确处理OPTIONS请求,可能会导致后续的POST请求被转为GET
  2. 解决方案

    // 在uni.request中明确指定method
    uni.request({
      url: '你的API地址',
      method: 'POST', // 明确指定POST方法
      data: {...},
      header: {
        'Content-Type': 'application/json' // 根据实际情况设置
      },
      success: (res) => {...}
    })
    
  3. 服务器端需要配置

    • 确保服务器正确处理OPTIONS预检请求
    • 在响应头中添加正确的CORS配置:
      Access-Control-Allow-Origin: *
      Access-Control-Allow-Methods: GET, POST, OPTIONS
      Access-Control-Allow-Headers: Content-Type
回到顶部