【发布时间】:2020-08-19 02:20:20
【问题描述】:
问题:调用使用 axios 的 vue 方法(通过@tap)时,出现以下错误: “错误:请求失败,状态码 429”
我在网站上使用相同的方法,它按预期工作(除了 async/await 部分,它是从建议中添加的 - 请参阅相关部分的评论)。
模拟器(Pixel 2 API 28)可以访问网络。
关于我的应用设置的信息:
@package.json:
"dependencies": {
"@nativescript/theme": "^2.2.1",
"@vue/devtools": "^5.3.3",
"axios": "^0.19.2",
"nativescript-socketio": "^3.3.1",
"nativescript-toasty": "^3.0.0-alpha.2",
"nativescript-vue": "^2.6.1",
"nativescript-vue-devtools": "^1.4.0",
"tns-core-modules": "^6.5.1",
"vuex": "^3.3.0"
}
@AndroidManifest.xml:
// Suggestion from stackoverflow:
// https://stackoverflow.com/questions/55184243/how-to-call-axios-api-call-in-nativescript-application
<application
android:usesCleartextTraffic="true"
@npm ls --depth=0
├── axios@0.19.2
├── vue@2.6.11
├── vue-axios@2.1.5
├── xmldom@0.3.0
└── xpath@0.0.27
@main.js:
import Vue from 'nativescript-vue'
import App from './components/App'
import VueDevtools from 'nativescript-vue-devtools'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueDevtools, VueAxios, axios)
@App.vue(页面模块):
// Also tried: import axios from 'axios/dist/axios'; - As suggested from stack overflow:
// https://stackoverflow.com/questions/55184243/how-to-call-axios-api-call-in-nativescript-application
import axios from 'axios';
[...]
methods: {
// The reason I've made it 'async', was from a suggestion on S.O., that axios might return Error: the 429 error,
// due to simultaneous calls.
// https://stackoverflow.com/questions/54254235/axios-request-failed-with-status-code-429-but-it-is-working-with-postman
fetchit: async function () {
let xmls='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\
xmlns:web="http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL">\
<soapenv:Header/>\
<soapenv:Body>\
<tns:GetWeatherInformation>\
<name:GetCityWeatherByZIP>65215</name:GetCityWeatherByZIP>\
</tns:GetWeatherInformation>\
</soapenv:Body>\
</soapenv:Envelope>';
try{
const result = await axios.get('https://translate.googleapis.com/translate_a/single?client=gtx&sl=el&tl=en&dt=t&q=πέντε')
.then(res=>{console.log("Sucess!", res);})
.catch(err=>{console.log("Here......................",err);})
return result;
}
catch (error) {
console.log("Try-Except Error = ", error);
}
}
}
我怀疑由于某种原因这不是编码问题,而是缺少模拟器对我的应用程序的许可。
提前感谢您的任何帮助/建议!
【问题讨论】:
-
正如错误提示的那样,您可能从 IP 访问 API 的次数过多,而 Google 可能会阻止您。
-
感谢您的洞察力,但是无论我尝试向哪个网站提出请求,我都会收到相同的错误。同样正如我所提到的,如果我在浏览器中从同一个 ip 运行相同的方法,它就可以工作。所以不可能是这样的。
-
我不认为这个端点是供开发人员使用的,你在哪里找到的。您是否浏览过翻译 REST API 文档?
-
只是为了测试。此外,它确实在浏览器中工作。但是让我们忘记谷歌翻译吧,即使我在任何其他网站上进行获取/发布,我仍然会遇到同样的错误,即使带有 Vue 的 html 在浏览器上完美运行。所有迹象都表明模拟器对来自应用程序的 http(s) 调用有任何问题。
-
我创建了一个小提琴(没有 async/await 部分,因为错误仍然相同)以表明它有效:只需在文本字段,然后按 Enter。它有效:jsfiddle.net/lopofsky/q7znbd1p/1
标签: android vue.js axios nativescript