文档没找到相关说明,查了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}`);
});