methods: {
uploadImage(filePath) {
var baseUrl = 'http://192.168.110.226:8000';
console.log(filePath);
uni.request({
url:baseUrl+"/test",
success: (res) => {
console.log(res);
},
fail: (error) => {
console.log(error);
}
})
uni.uploadFile({
url: baseUrl +'/upload', // 修改为你的FastAPI服务器地址
filePath: filePath,
name: 'image', // 后端接收的文件名,确保与FastAPI端的路由中一致
success: (uploadFileRes) => {
console.log(uploadFileRes);
const data = JSON.parse(uploadFileRes.data);
console.log('Upload response:', data);
this.description = data.file_location
},
fail: (error) => {
console.error('Upload failed:', error);
uni.showToast({
title: '上传失败',
icon: 'none'
});
}
});
},
}
- 发布:2024-05-11 14:09
- 更新:2024-05-30 11:11
- 阅读:376
【报Bug】uniapp模拟器请求报错localhost和局域网ip都报错"request:fail abort statusCode:-1 Failed to connect to
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: Win11
HBuilderX类型: 正式
HBuilderX版本号: 4.15
手机系统: Android
手机系统版本号: Android 7.0
手机厂商: OPPO
手机机型: 夜神模拟器
页面类型: vue
vue版本: vue2
打包方式: 云端
项目创建方式: HBuilderX
示例代码:
操作步骤:
methods: {
uploadImage(filePath) {
var baseUrl = 'http://192.168.110.226:8000';
console.log(filePath);
uni.request({
url:baseUrl+"/test",
success: (res) => {
console.log(res);
},
fail: (error) => {
console.log(error);
}
})
uni.uploadFile({
url: baseUrl +'/upload', // 修改为你的FastAPI服务器地址
filePath: filePath,
name: 'image', // 后端接收的文件名,确保与FastAPI端的路由中一致
success: (uploadFileRes) => {
console.log(uploadFileRes);
const data = JSON.parse(uploadFileRes.data);
console.log('Upload response:', data);
this.description = data.file_location
},
fail: (error) => {
console.error('Upload failed:', error);
uni.showToast({
title: '上传失败',
icon: 'none'
});
}
});
},
}
methods: {
uploadImage(filePath) {
var baseUrl = 'http://192.168.110.226:8000';
console.log(filePath);
uni.request({
url:baseUrl+"/test",
success: (res) => {
console.log(res);
},
fail: (error) => {
console.log(error);
}
})
uni.uploadFile({
url: baseUrl +'/upload', // 修改为你的FastAPI服务器地址
filePath: filePath,
name: 'image', // 后端接收的文件名,确保与FastAPI端的路由中一致
success: (uploadFileRes) => {
console.log(uploadFileRes);
const data = JSON.parse(uploadFileRes.data);
console.log('Upload response:', data);
this.description = data.file_location
},
fail: (error) => {
console.error('Upload failed:', error);
uni.showToast({
title: '上传失败',
icon: 'none'
});
}
});
},
}
预期结果:
能返回接口信息
能返回接口信息
实际结果:
"errMsg": "request:fail abort statusCode:-1 Failed to connect to /127.0.0.1:8000"
"errMsg": "request:fail abort statusCode:-1 Failed to connect to /127.0.0.1:8000"
bug描述:
uniapp模拟器请求报错,localhost或者是局域网ip都报错,更换了网络环境也不行
localhost:"errMsg": "request:fail abort statusCode:-1 Failed to connect to /127.0.0.1:8000"
局域网IPv4报错"errMsg": "request:fail abort statusCode:-1 Failed to connect to /192.168.110.226:8000"
我本地后端确定在跑也接受不到请求
h5能链接,但是app模拟器里面不行,但是之前hbuider版本可以直接调用localhost,更新新版本后无法调用
2***@qq.com (作者)
解决了,后端就是0.0.0.0,前端就用局域网ip
2024-06-17 21:56