XMLHttpRequest cannot load http://localhost:8080/api/test. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
我阅读了有关跨域ajax请求的信息,并了解了基础安全问题.就我而言,2台服务器在本地运行,并且喜欢在测试期间启用跨域请求.
localhost:8080 - Google Appengine dev server
localhost:3000 - Node.js server
Run Code Online (Sandbox Code Playgroud)
我正在localhost:8080 - GAE server从节点服务器加载我的页面时发出一个ajax请求.什么是最简单,最安全的(不想用disable-web-security选项启动chrome ).如果我必须改变'Content-Type',我应该在节点服务器上做吗?怎么样?
我正在尝试在node.js中构建一个支持跨域脚本的Web服务器,同时仍然提供来自公共目录的静态文件.我正在使用express.js,我不确定如何允许跨域脚本(Access-Control-Allow-Origin: *).
我看到这篇文章,我觉得没有用.
var express = require('express')
, app = express.createServer();
app.get('/', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.configure(function () {
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
});
app.configure('development', function () {
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function () {
var oneYear = 31557600000;
// app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler());
});
app.listen(8888);
console.log('express running at http://localhost:%d', 8888);
Run Code Online (Sandbox Code Playgroud) 我想在我的角度项目中使用http://5.160.2.148:8091/api/trainTicketing/city/findAll rest 来获取城市。
我在我的项目中使用了 7.2.15 版的 angular。
当使用 httpClient 获取此 url 时抛出以下错误:
Access to XMLHttpRequest at 'http://5.160.2.148:8091/api/trainTicketing/city/findAll' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)
在浏览器和邮递员中输入 url 时正常工作。
为什么 ?
我遇到了ajax请求的麻烦.我收到了错误
No 'Access-Control-Allow-Origin' header is present on the requested resource.
所以我尝试的是这个jQuery ajax请求:
var request = $.ajax({
type: 'GET',
url: url,
dataType: "json",
xhrFields: {
withCredentials: true
}
});
request.done(function(data){
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
但它仍然无法正常工作.我仍然得到错误.
我该怎么解决这个问题?
在尝试使用NodeJS/Express中的Passport对Facebook用户进行身份验证时,已经有两天和一百万次尝试启用CORS.
我在Chrome上遇到的错误是:
XMLHttpRequest cannot load https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%…%3A8080%2Fauth%2Ffacebook%2Fcallback&scope=email&client_id=598171076960591.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8080' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
我使用的路线很简单:
// =====================================
// FACEBOOK ROUTES =====================
// =====================================
// route for facebook authentication and login
app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));
// handle the callback after facebook has authenticated the user
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/home',
failureRedirect : '/login'
}));
Run Code Online (Sandbox Code Playgroud)
这是在我的angularJS文件上调用路由的方式(我也尝试过设置withCredentials:true):
$http.get('/auth/facebook')
.success(function(response) {
}).error(function(response){
});
Run Code Online (Sandbox Code Playgroud)
我在StackOverflow和其他论坛上尝试了十几个解决方案.
我尝试在routes.js文件的路由之前添加它:
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*'); …Run Code Online (Sandbox Code Playgroud)res.setHeader和res.header有什么区别.哪一个应该用于启用CORS?在某些页面中使用了res.header,并且一些页面res.setHeader用于CORS.
我有一个Backbone Collection对象,其URL为"http:// localhost:8080/api/menu/1/featured".我正在尝试执行获取操作以从URL检索集合并解析它.但是,在服务器端,我为此请求看到的方法类型是OPTIONS.服务器只是支持GET方法.我不确定Backbone如何确定要使用的方法类型,以及为什么它有时会随机更改为OPTIONS方法类型.我正在使用Node.js服务器来处理请求.下面的代码几乎就是我所做的.
var FeaturedCollection = Backbone.Collection.extend({
model:FeaturedContent,
url:function () { return url_featured; },
parse:function (response) {
console.log(response);
return response;
}
});
var featuredCollection = new FeaturedCollection();
featuredCollection.fetch();
Run Code Online (Sandbox Code Playgroud)
请帮忙,谢谢!
我正在运行一个简单的 API 请求,将数据返回到我编写的一个简单的 API 搜索中。我说这是简单的 API 调用,因为不需要身份验证,我可以非常简单地在 python 中完成。
但是,我在 React 中使用 Axios 时遇到了问题。
代码:
axios.get('https://www.keyforgegame.com/api/decks/59554ab7-b414-4220-8514-408a0dbf572d')
Run Code Online (Sandbox Code Playgroud)
我试过看这里,每个人都让它听起来很简单,但我似乎什么也做不了。我试过了。
axios.get('https://www.keyforgegame.com/api/decks/59554ab7-b414-4220-8514-408a0dbf572d', { crossDomain: true })
Run Code Online (Sandbox Code Playgroud)
和
axios.get('https://www.keyforgegame.com/api/decks/59554ab7-b414-4220-8514-408a0dbf572d, {
headers: {
'Access-Control-Allow-Origin': true,
},
})
Run Code Online (Sandbox Code Playgroud)
但我不断收到类似的错误
Access to XMLHttpRequest at 'https://www.keyforgegame.com/api/decks/59554ab7-b414-4220-8514-408a0dbf572d' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
或者
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我是否需要在标题中放一些东西才能完成这项工作?或者这是我需要做出反应的某种设置。
帮助!
我正在我的本地电脑上开发一个应用程序.应该使用spinejs构建前端,使用node.js构建后端api.Spine在端口9294上运行,node.js在端口3000上运行.在Spine中我已将以下内容添加到我的模型中:
@url: "http:localhost:3000/posts"
Run Code Online (Sandbox Code Playgroud)
在我的快递服务器上
app.get('/posts', function(req, res){
console.log("giving ALL the posts");
res.header("Access-Control-Allow-Origin", "*")
res.json(posts);
});
Run Code Online (Sandbox Code Playgroud)
但我总是在chrome中获得以下错误:
XMLHttpRequest cannot load http://localhost:3000/posts. Origin http://localhost:9294 is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能正确访问我的api?我虽然在响应中添加标题确实解决了问题.
我从node.js服务器获取数据时遇到问题.
客户端是:
public getTestLines() : Observable<TestLine[]> {
let headers = new Headers({ 'Access-Control-Allow-Origin': '*' });
let options = new RequestOptions({ headers: headers });
return this.http.get('http://localhost:3003/get_testlines', options)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
Run Code Online (Sandbox Code Playgroud)
在服务器端我也设置了标题:
resp.setHeader('Access-Control-Allow-Origin','*')
resp.send(JSON.stringify(results))
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个错误
"XMLHttpRequest无法加载http:// localhost:3003/get_testlines.对预检请求的响应未通过访问控制检查:请求的资源上没有'Access-Control-Allow-Origin'标头.来源' http://因此不允许localhost:3000 '访问."
我该如何解决?当我删除标题时,它表示此标题是必需的.
cors ×5
node.js ×5
ajax ×3
javascript ×3
jquery ×3
angular ×2
express ×2
angularjs ×1
axios ×1
backbone.js ×1
cross-domain ×1
fetch ×1
html ×1
http-headers ×1
options ×1
passport.js ×1
reactjs ×1
request ×1
spine.js ×1
webserver ×1