正常引用 import axios from “…/lib/axios.js”, 并将axios.js设置为插件脚本,常规使用 axios.get(‘http://localhost:3000’).then((response) => {console.log(response);}).catch((error) => {console.log(error);});
但是会报错
请问大神,该怎么解决??? 多谢啦
正常引用 import axios from “…/lib/axios.js”, 并将axios.js设置为插件脚本,常规使用 axios.get(‘http://localhost:3000’).then((response) => {console.log(response);}).catch((error) => {console.log(error);});
但是会报错
请问大神,该怎么解决??? 多谢啦
看起来是他库里面的问题
我知道axios模拟器上不能使用是因为"node_modules/axios/lib/utils.js"这个文件里
function isStandardBrowserEnv() {
if (typeof navigator !== 'undefined' && (navigator.product === 'ReactNative' ||
navigator.product === 'NativeScript' ||
navigator.product === 'NS')) {
return false;
}
return (
typeof window !== 'undefined' &&
typeof document !== 'undefined'
);
}
把模拟器判断成浏览器模式
所以"node_modules/axios/lib/helpers/isURLSameOrigin.js"中的"resolveURL"使用了"window.location.href"导致
function resolveURL(url) {
var href = url;
if (msie) {
// IE needs attribute set twice to normalize properties
urlParsingNode.setAttribute('href', href);
href = urlParsingNode.href;
}
urlParsingNode.setAttribute('href', href);
// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
return {
href: urlParsingNode.href,
protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
host: urlParsingNode.host,
search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
hostname: urlParsingNode.hostname,
port: urlParsingNode.port,
pathname: (urlParsingNode.pathname.charAt(0) === '/') ?
urlParsingNode.pathname :
'/' + urlParsingNode.pathname
};
}
传入的url是"./game.js"
urlParsingNode.pathname
为空, 会导致用不了.
但是我看你也不是模拟器啊, 是浏览器, 应该能正常使用啊
我这是微信开发者工具, 是的, 模拟器也会报这个错。
有解决方法或者思路嘛? 朋友
他是判断是否同源写cookies, 你应该这么判断
if (typeof navigator !== 'undefined' && typeof navigator.product !== 'undefined') { // 可能还需要其他判断来保证不影响其他平台
Object.defineProperty(navigator, "product" , {value: "NativeScript"});
}
我不知道微信小程序的navigator.product
的值是啥,如果navigator.product
有值的话那就强制写死isStandardBrowserEnv的返回值, 如果navigator.product
没有值就用这个判断
https://mp.weixin.qq.com/s/O4hYJ9u0SSSCpyI58k5G1w
文章我在最后记录了这个问题
其实,我用过一段时间的axios,但是我放弃了,后来它又出现了别的问题,最后还是自己封装了一套使用
你不能太暴力了,写代码还是要理解其中的原理,理解代码作者的用意, 这样你才好修改,不然鬼会知道会影响到什么
嗯,爱了爱了,多谢~
多谢多谢
多谢推荐, 我看看
想问一下你这个支持原生吗?
调用报错了 什么情况?
我的解决方案是放弃 axios,真难受。还是用 xhr 吧
我也是2.4.2,也是用的axios,好像没什么问题啊
cocos 里没问题,网页没问题,打包 android 就完犊子了
我上边不是发了解决办法了么····
我公众号里也发过