下面给出的API正在使用本地地址,但在上传到Github后显示错误。
我的代码:
cons = function(){
this.xhr = new XMLHttpRequest()
}
//GET
cons.prototype.get = function(url,callback){
this.xhr.open('GET',url,true)
let self
self = this.xhr
this.xhr.onload = function(){
if(self.status===200){
callback(null,JSON.parse(self.responseText))
}
else{
callback(`error found ${self.status}`)
}
}
this.xhr.setRequestHeader("x-rapidapi-host", "baconator-bacon-ipsum.p.rapidapi.com");
this.xhr.setRequestHeader("x-rapidapi-key", "api-key");
this.xhr.send()
}
我的错误:
访问XMLHttpRequest'https://baconator-bacon-ipsum.p.rapidapi.com/?sentences=0&第1&以lorem=0&type=所有肉来自原点'https://nikhildeveloper.github.io'已被阻止CORS策略:"Access Control Allow Origin"标头具有值'http://127.0.0.1:5500',该值不等于提供的原点。
CORS需要在后端进行配置。您的后端需要允许glthub域的CORS
它在您的本地环境中工作的原因是它们都在同一个域(通常是localhost(下工作。