我正在运行带有文本框和提交按钮的 vue 脚本,我正在调用API,以将我在文本框中写的内容发布到API为了从API返回信息,我在标题事件中提到的错误,尽管我已经写了JavaScript函数,但它应该是应该的?
使用脚本,我首先设置了一个新的xmlhttprequest,启动了get和post方法的标题和API键。然后,我创建了2个函数,以从文本框中获取数据并将其发送到API,然后使用另一个功能进行另一个按钮以发送数据。
我经历了这种方法,因为我一直在遇到CORS问题,并且API需要我声明访问控制起源标题,此代码我做错了什么?任何帮助将不胜感激
<script>
export default {
name: 'ProperForm'
}
methods: {
StartClient: function () {
this.get = function(Url, Callback){
var aHttpRequest = new XMLHttpRequest();
aHttpRequest.onreadystatechange = function() {
if (aHttpRequest.readyState == 4 && aHttpRequest.status == 200)
Callback(aHttpRequest.responseText);
}
aHttpRequest.open("GET", Url, true);
aHttpRequest.setRequestHeader("X-Api-Key", "eVnbxBPfn01kuoJIdfgi46TiYNv8AIip1r3WbjsX");
aHttpRequest.send(null);
}
this.post = function(Url, message, Callback) {
var aHttpRequest = new XMLHttpRequest();
aHttpRequest.onreadystatechange = function() {
if (aHttpRequest.readyState == 4 && aHttpRequest.status == 200)
Callback(aHttpRequest.responseText);
}
aHttpRequest.open("POST", Url, true);
aHttpRequest.setRequestHeader("x-api-key", "eVnbxBPfn01kuoJIdfgi46TiYNv8AIip1r3WbjsX");
aHttpRequest.send(message);
}
}
var client = new StartClient();
submitData: function () {
document.getElementById('inputBox').disabled = true;
var targetInputButton = document.getElementById("inputBox").value;
var message = '{"targetInputButton":"' + targetInputButton + '"}';
client.post('https://le75bkfcmg.execute-api.eu-west-2.amazonaws.com/dev/start-trace', message, function(response) {
document.getElementById('jobId').innerHTML = response;
});
}
sendBackData: function () {
var jobId = document.getElementById("jobId").innerHTML;
var message = '{"jobId":"' + jobId + '"}';
client.post('https://le75bkfcmg.execute-api.eu-west-2.amazonaws.com/dev/check-trace', message, function(response) {
document.getElementById('report').innerHTML = response;
});
}
}
</script>
我编写了var客户端的新方法:
StartClient: function () {
var client
},
您需要将方法对象放入导出中,然后将方法拆分为comma
<script>
export default {
name: 'name',
methods:{
foo(){
},
bar(){
}
}
}
UPD:var client = new StartClient();
在方法之外定义