2***@qq.com
2***@qq.com
  • 发布:2025-06-09 21:53
  • 更新:2025-06-10 09:06
  • 阅读:57

【新手求助】uni.request 设置 自定义header不生效, h5和安卓模拟器均没有带上

分类:uni-app

文档没找到相关说明,查了1个小时也没结果,求助!!!

<template>  
    <view class="content">  
        <image class="logo" src="/static/logo.png"></image>  
        <view class="text-area">  
            <text class="title">{{title}}</text>  
            <button @click="test">请求</button>  
        </view>  
    </view>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
                title: 'Hello'  
            }  
        },  
        onLoad() {  

        },  
        methods: {  
            async test() {  
                const apiUrl = 'http://192.168.31.166:3001';  

                // 精简请求头  
                const headers = {  
                  'test1': '123',  
                };  
                const res = await uni.request({  
                  url: apiUrl,  
                  method: 'GET',  
                  headers: headers  
                });  
                console.log('接口响应:', res);  
            }  
        }  
    }  
</script>
const http = require('http');  

const server = http.createServer((req, res) => {  
  // 设置CORS头部,允许所有域名跨域访问  
  res.setHeader('Access-Control-Allow-Origin', '*');  
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');  
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, test1');  

  // 处理预检请求  
  if (req.method === 'OPTIONS') {  
    res.writeHead(204);  
    res.end();  
    return;  
  }  

  // 返回请求头信息  
  res.writeHead(200, { 'Content-Type': 'application/json' });  
  res.end(JSON.stringify(req.headers, null, 2));  
});  

const port = 3001;  
server.listen(port, '0.0.0.0', () => {  
  console.log(`服务器运行在 http://localhost:${port}`);  
});
2025-06-09 21:53 负责人:无 分享
已邀请:
爱豆豆

爱豆豆 - 办法总比困难多

写错了吧 是header不是headers 我测试是可以加上header的
参考文档:https://uniapp.dcloud.net.cn/api/request/request.html

要回复问题请先登录注册