我试图通过连接到Flask内置的RESTful API来使用JavaScript进行授权.但是,当我发出请求时,我收到以下错误:
XMLHttpRequest无法加载http:// myApiUrl/login.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许原点'null'访问.
我知道API或远程资源必须设置标题,但为什么在我通过Chrome扩展程序Postman发出请求时它可以正常工作?
这是请求代码:
$.ajax({
type: "POST",
dataType: 'text',
url: api,
username: 'user',
password: 'pass',
crossDomain : true,
xhrFields: {
withCredentials: true
}
})
.done(function( data ) {
console.log("done");
})
.fail( function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
alert(textStatus);
});
Run Code Online (Sandbox Code Playgroud) 我正在创建一个在其他网站上外部加载的脚本.它加载CSS和HTML,并在我自己的服务器上正常工作.
但是,当我在另一个网站上尝试它时,它会显示这个可怕的错误:
Access-Control-Allow-Origin
Run Code Online (Sandbox Code Playgroud)
在这里你可以看到它完美加载:http://tzook.info/bot/
但在其他网站上显示错误:http: //cantloseweight.co/robot/
我将加载脚本上传到jsfiddle:http: //jsfiddle.net/TL5LK/
我尝试像这样编辑htaccess文件:
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin *
</IfModule>
Run Code Online (Sandbox Code Playgroud)
或者像这样:
Header set Access-Control-Allow-Origin *
Run Code Online (Sandbox Code Playgroud)
但它仍然无效.
我正在构建一个chrome扩展程序,它将读取用户的电子邮件并检查它们是否存在拼写错误.但是,当我尝试在我的background.js中验证用户时,我遇到了这个错误:
uO {message:"无效的cookiePolicy",堆栈:"gapi.auth2.ExternallyVisibleError:无效的cookieP ...在handleResponse(extensions :: sendRequest:67:7)"}
这是我试图验证它们的方式:
background.js
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://apis.google.com/js/client.js?onload=callbackFunction";
head.appendChild(script);
chrome.identity.getAuthToken({interactive: true}, authorize);
function authorize(token) {
gapi.auth.authorize({
client_id: '800382879116-k3luktdc1lmb1e1fml8i8u.apps.googleusercontent.com',
immediate: true,
scope: 'https://www.googleapis.com/auth/gmail.readonly'
},
function(result){
console.log(result);
gapi.client.load('gmail', 'v1', callback);
});
}
Run Code Online (Sandbox Code Playgroud)
background.html
<!DOCTYPE html>
<html>
<body>
<script src='scripts/background.js'></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
的manifest.json
{
"name": "Gmail Typo Analyzer",
"version": "0.1",
"description": "Gmail Typo Analyzer",
"permissions": [
"identity",
"storage"
],
"content_security_policy": "script-src 'self' https://apis.google.com; object-src 'self'",
"oauth2": {
"client_id": "82879116-k3luktdc1li8u.apps.googleusercontent.com", …
Run Code Online (Sandbox Code Playgroud) 我在提出客户请求时遇到问题。
我遵循了Nuxt.js和Axios上的文档,但我似乎仍然无法让它工作。也许我错过了一些东西..
我的 Vue 组件调用vuex 操作:
methods: {
open() {
this.$store.dispatch('events/getEventAlbum');
}
}
Run Code Online (Sandbox Code Playgroud)
该行动在vuex:
export const actions = {
async getEventAlbum(store) {
console.log('album action');
const response = await Axios.get(url + '/photos?&sign=' + isSigned + '&photo-host=' + photoHost);
store.commit('storeEventAlbum', response.data.results);
}
};
Run Code Online (Sandbox Code Playgroud)
还有我的nuxt.js.config
modules: [
'@nuxtjs/axios',
'@nuxtjs/proxy'
],
axios: {
proxy: true
},
proxy: {
'/api/': { target: 'https://api.example.com/', pathRewrite: {'^/api/': ''} }
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供帮助?
cors ×3
javascript ×3
.htaccess ×1
ajax ×1
cross-domain ×1
google-api ×1
jquery ×1
nuxt.js ×1
vuejs2 ×1
vuex ×1