我使用ngResource在Amazon Web Services上调用REST API时收到此错误:
XMLHttpRequest无法加载 http://server.apiurl.com:8000/s/login?login=facebook.对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.原产地" :HTTP //本地主机,因此"是不允许访问. 错误405
服务:
socialMarkt.factory('loginService', ['$resource', function($resource){
var apiAddress = "http://server.apiurl.com:8000/s/login/";
return $resource(apiAddress, { login:"facebook", access_token: "@access_token" ,facebook_id: "@facebook_id" }, {
getUser: {method:'POST'}
});
}]);
Run Code Online (Sandbox Code Playgroud)
控制器:
[...]
loginService.getUser(JSON.stringify(fbObj)),
function(data){
console.log(data);
},
function(result) {
console.error('Error', result.status);
}
[...]
Run Code Online (Sandbox Code Playgroud)
我正在使用Chrome,我不知道还有什么可以解决这个问题.我甚至将服务器配置为接受来自源的头文件localhost.
我正在尝试创建一个代理服务器,将HTTP GET客户端的请求传递给第三方网站(比如google).我的代理只需要将传入的请求镜像到目标站点上的相应路径,因此如果我的客户端请求的URL是:
127.0.0.1/images/srpr/logo11w.png
Run Code Online (Sandbox Code Playgroud)
应提供以下资源:
http://www.google.com/images/srpr/logo11w.png
Run Code Online (Sandbox Code Playgroud)
这是我想出的:
http.createServer(onRequest).listen(80);
function onRequest (client_req, client_res) {
client_req.addListener("end", function() {
var options = {
hostname: 'www.google.com',
port: 80,
path: client_req.url,
method: client_req.method
headers: client_req.headers
};
var req=http.request(options, function(res) {
var body;
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
client_res.writeHead(res.statusCode, res.headers);
client_res.end(body);
});
});
req.end();
});
}
Run Code Online (Sandbox Code Playgroud)
它适用于html页面,但对于其他类型的文件,它只返回一个空白页面或来自目标站点的一些错误消息(在不同的站点中有所不同).
我的 Angular 2 Web 应用程序出现问题。
在 Node JS 服务器端,我遇到了 CORS 预检的问题。
我想在服务器上上传一个文件,当我这样做时,我遇到了这个问题:
XMLHttpRequest 无法加载http://localhost:4000/upload。对预检请求的响应未通过访问控制检查:当请求的凭据模式为“包含”时,响应中“Access-Control-Allow-Origin”标头的值不得为通配符“*”。因此,不允许访问Origin ' http://localhost:3000 '。XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
其中 localhost:4000 是我的服务器, localhost:3000 是我的客户端。
我的server.js文件是这样的:
require('rootpath')();
var express = require('express');
var app = express();
var cors = require('cors');
var bodyParser = require('body-parser');
var expressJwt = require('express-jwt');
var config = require('config.json');
var multer = require('multer');
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// // use JWT auth to secure the api
app.use(expressJwt({ secret: config.secret }).unless({ path: ['/users/authenticate', …Run Code Online (Sandbox Code Playgroud) 我遇到过一种情况,我从 Angular 6 应用程序调用 Spring Boot 应用程序。当我以与在不同端口上运行的应用程序的角度调用 HTTP post 方法时,它会引发异常。
从源“ http://localhost: 4200 ”访问位于“http://localhost:8080/api”的XMLHttpRequest已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP好的状态。
我的 Angular 应用程序在“ http://localhost:4200 ”上运行,端口号:4200 我的 Spring Boot 应用程序在“ http://localhost:8080/api ”上运行:端口号:8080。
没有直接的答案可以解决这个问题。有一些技巧可以禁用 Chrome 中的安全性,使用 NGINX 可以解决此问题。
cors ×3
angular ×2
javascript ×2
node.js ×2
ajax ×1
angular6 ×1
cross-domain ×1
http ×1
proxy ×1
spring-boot ×1