我使用Node/Express创建了一个小API并尝试使用Angularjs来提取数据,但是当我的html页面在localhost:8888上运行apache并且节点API在端口3000上侦听时,我得到了No'Access-Control-允许来源.我尝试使用node-http-proxy和Vhosts Apache但没有取得多大成功,请参阅下面的完整错误和代码.
"XMLHttpRequest无法加载localhost:3000.请求的资源上没有'Access-Control-Allow-Origin'标头.因此不允许来自"localhost:8888"."
// Api Using Node/Express
var express = require('express');
var app = express();
var contractors = [
{
"id": "1",
"name": "Joe Blogg",
"Weeks": 3,
"Photo": "1.png"
}
];
app.use(express.bodyParser());
app.get('/', function(req, res) {
res.json(contractors);
});
app.listen(process.env.PORT || 3000);
console.log('Server is running on Port 3000')
Run Code Online (Sandbox Code Playgroud) 我有客户端和服务器在不同的端口上运行.服务器正在运行Web API 2(v5.0.0-rc1).
我尝试安装Microsoft ASP.NET Web API跨源支持包并启用它WebApiConfig.cs.它给了我EnableCors()功能,所以包正确安装.
在这里你可以看到我的Register()功能WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
}
Run Code Online (Sandbox Code Playgroud)
GET请求工作正常.但在发送时POST,我得到以下内容:
OPTIONS http://localhost:19357/api/v1/rooms? 404 (Not Found) angular.js:10159
OPTIONS http://localhost:19357/api/v1/rooms? Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin. angular.js:10159
XMLHttpRequest cannot load http://localhost:19357/api/v1/rooms. Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
根据Fiddler的说法,它只发送OPTIONS请求.POST之后不发布.
所以我猜测config.EnableCors(cors);在WebApiConfig.cs没有做任何事情,这导致服务器拒绝客户端/浏览器发送POST请求.
你知道如何解决这个问题吗? …
我从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 '访问."
我该如何解决?当我删除标题时,它表示此标题是必需的.
我试图在服务中使用Angular 2(2.0.0-rc.1)进行简单的Check Login方法.当我尝试将内容类型设置为application/json时,它在发送到我的web api后端时从不设置标题.
import { Injectable } from '@angular/core';
import { HTTP_PROVIDERS, Http, Response, Headers, RequestOptionsArgs, RequestMethod, Request, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Other Modules Left out for Security } from 'Hidden';
@Injectable()
export class LoginService {
private _http: Http;
constructor(http: Http) {
this._http = http;
}
CheckLogin(model: CredentialModel) {
let url = 'http://localhost:51671/api/Login';
let data = JSON.stringify(model);
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
let requestOptions = new RequestOptions({ …Run Code Online (Sandbox Code Playgroud) 我使用正确的凭据点击 Key cloak api http://localhost:8080/auth/realms/ **/protocol/openid-connect/token ,它工作正常,但凭据错误
当我添加 cross-origin-allow 时,它会给我预检错误
任何人都可以帮助我:) PS:使用 CORS 插件一切正常