- 发布:2022-06-29 17:01
- 更新:2022-07-01 09:56
- 阅读:378
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: 19044.1381
HBuilderX类型: 正式
HBuilderX版本号: 3.4.15
手机系统: 全部
手机厂商: 华为
手机机型: mate30
页面类型: nvue
vue版本: vue2
打包方式: 云端
项目创建方式: HBuilderX
测试过的手机:
示例代码:
import service from 'axios';
import qs from 'qs';
import axiosAdapterUniapp from 'axios-adapter-uniapp'
const str = ""
//==================H5===============begin
/ #ifdef H5 /
str = ""
service.defaults.baseURL = "/api";
/ #endif /
//==================H5===============end
//==================MP-WEIXIN===============begin
/ #ifdef MP-WEIXIN/
str = "https://**"
service.defaults.baseURL = "";
/ #endif /
//==================MP-WEIXIN===============end
//==================APP===============begin
/ #ifdef APP-PLUS /
service.defaults.baseURL = "";
// 正式环境
str = "https://www.****server/"
// 测试环境
// const str = "http://localhost:6999/mserver/"
service.defaults.adapter = function (config) {
return new Promise((resolve, reject) => {
console.log(config)
var settle = require('axios/lib/core/settle');
var buildURL = require('axios/lib/helpers/buildURL');
uni.request({
method: config.method.toUpperCase(),
url: buildURL(config.url, config.params, config.paramsSerializer),
header: config.headers,
data: config.data,
dataType: config.dataType,
responseType: config.responseType,
sslVerify: config.sslVerify,
complete:function complete(response){
response = {
data: response.data,
status: response.statusCode,
errMsg: response.errMsg,
header: response.header,
config: config
};
settle(resolve, reject, response);
}
})
})
}
/ #endif /
//==================APP===============end
// request拦截器,在请求之前做一些处理
service.interceptors.request.use(
(config) => {
config.headers["X-Requested-With"] = "XMLHttpRequest";
config.headers["Content-Type"] = "application/json; charset=UTF-8";
if (!config.data) return config;
if (config.data.isUpload) config.headers["Content-Type"] = "multipart/form-data";
return config;
},
(error) => {
console.log(error);
return Promise.reject(error);
}
);
//配置成功后的拦截器
service.interceptors.response.use(
(response) => {
const data = response.data;
if (Object.prototype.hasOwnProperty.call(data, "code")) {
// 程序异常 = -2,
// 未授权 = -1,
// 失败 = 0,
// 成功 = 1,
if (data.code === -1) {
//接口授权码无效
tools.message(data.message + ",请重新登录授权!", "警告");
// return router.push("/login");
}
if (data.code === -2) {
//服务端异常
console.log("错误",data.message)
}
if (data.code === 0) {
//失败
console.log("警告",data.message)
}
}
return response;
},
(error) => {
if (error.response) {
if (error.response.status === 401) {
console.log("未授权!", "错误")
// router.replace("/login");
// window.location.reload();
}
}
if (error.message === 'Network Error') {
console.log("网络连接错误!", "错误")
// tools.message('网络连接错误!', "错误");
// router.replace("/login");
}
console.log(error);
return Promise.reject(error.response.data);
}
);
export default {
/**
- @param {Object} url
- @param {Object} params params: {selectDate: selectDate,flightId: flightId},
-
@param {Object} data {}
*/
get(url, params, data) {
return new Promise((resolve, reject) => {
service({
method: "get",
url: str+url,
params: params,
data: data
// data:this.qs.stringify({ //这里是发送给后台的数据
// userId:this.userId,
// token:this.token,
// })
}).then((response) => { //这里使用了ES6的语法
console.log("request======:",
response) //请求成功返回的数据
resolve(response);
}).catch((error) => {
reject(error)
// console.log("error",error) //请求失败返回的数据
})
});
}
}
import service from 'axios';
import qs from 'qs';
import axiosAdapterUniapp from 'axios-adapter-uniapp'
const str = ""
//==================H5===============begin
/ #ifdef H5 /
str = ""
service.defaults.baseURL = "/api";
/ #endif /
//==================H5===============end
//==================MP-WEIXIN===============begin
/ #ifdef MP-WEIXIN/
str = "https://**"
service.defaults.baseURL = "";
/ #endif /
//==================MP-WEIXIN===============end
//==================APP===============begin
/ #ifdef APP-PLUS /
service.defaults.baseURL = "";
// 正式环境
str = "https://www.****server/"
// 测试环境
// const str = "http://localhost:6999/mserver/"
service.defaults.adapter = function (config) {
return new Promise((resolve, reject) => {
console.log(config)
var settle = require('axios/lib/core/settle');
var buildURL = require('axios/lib/helpers/buildURL');
uni.request({
method: config.method.toUpperCase(),
url: buildURL(config.url, config.params, config.paramsSerializer),
header: config.headers,
data: config.data,
dataType: config.dataType,
responseType: config.responseType,
sslVerify: config.sslVerify,
complete:function complete(response){
response = {
data: response.data,
status: response.statusCode,
errMsg: response.errMsg,
header: response.header,
config: config
};
settle(resolve, reject, response);
}
})
})
}
/ #endif /
//==================APP===============end
// request拦截器,在请求之前做一些处理
service.interceptors.request.use(
(config) => {
config.headers["X-Requested-With"] = "XMLHttpRequest";
config.headers["Content-Type"] = "application/json; charset=UTF-8";
if (!config.data) return config;
if (config.data.isUpload) config.headers["Content-Type"] = "multipart/form-data";
return config;
},
(error) => {
console.log(error);
return Promise.reject(error);
}
);
//配置成功后的拦截器
service.interceptors.response.use(
(response) => {
const data = response.data;
if (Object.prototype.hasOwnProperty.call(data, "code")) {
// 程序异常 = -2,
// 未授权 = -1,
// 失败 = 0,
// 成功 = 1,
if (data.code === -1) {
//接口授权码无效
tools.message(data.message + ",请重新登录授权!", "警告");
// return router.push("/login");
}
if (data.code === -2) {
//服务端异常
console.log("错误",data.message)
}
if (data.code === 0) {
//失败
console.log("警告",data.message)
}
}
return response;
},
(error) => {
if (error.response) {
if (error.response.status === 401) {
console.log("未授权!", "错误")
// router.replace("/login");
// window.location.reload();
}
}
if (error.message === 'Network Error') {
console.log("网络连接错误!", "错误")
// tools.message('网络连接错误!', "错误");
// router.replace("/login");
}
console.log(error);
return Promise.reject(error.response.data);
}
);
export default {
/**
- @param {Object} url
- @param {Object} params params: {selectDate: selectDate,flightId: flightId},
-
@param {Object} data {}
*/
get(url, params, data) {
return new Promise((resolve, reject) => {
service({
method: "get",
url: str+url,
params: params,
data: data
// data:this.qs.stringify({ //这里是发送给后台的数据
// userId:this.userId,
// token:this.token,
// })
}).then((response) => { //这里使用了ES6的语法
console.log("request======:",
response) //请求成功返回的数据
resolve(response);}).catch((error) => { reject(error) // console.log("error",error) //请求失败返回的数据 }) });
}
}
操作步骤:
引入 import service from 'axios' 就报错
引入 import service from 'axios' 就报错
预期结果:
正常使用 import service from 'axios';
正常使用 import service from 'axios';
实际结果:
reportJSException >>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->Uncaught SyntaxError: Unexpected token ','
reportJSException >>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->Uncaught SyntaxError: Unexpected token ','
bug描述:
刚升的builderx 3.4.15.20220610 出现如下错误:
reportJSException >>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->Uncaught SyntaxError: Unexpected token ','
逐行删除代码发现是引用 import service from 'axios';报的错误,删除此行代码就不白屏了
2 个回复
b***@163.com (作者)
这段代码很久没有改过了,一直可以使用
phoeniix - Hello World
看看是不是我遇到的这个问题:https://ask.dcloud.net.cn/question/146906?notification_id-1104862rf-falseitem_id-203339__answer_id-203339__single-TRUE#!answer_203339