简体   繁体   English

Google Chrome浏览器中API调用的跨域错误

[英]Cross orgin error in google chrome for the api call

in the nodejs backend i have added this code to the server.js 在nodejs后端中,我已将此代码添加到server.js

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

but in angularjs 2 client side in google chrome is throwing this error 但是在谷歌浏览器的angularjs 2客户端中抛出此错误

XMLHttpRequest cannot load http://localhost:8080/team. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405.

this is the service code for angular2 i'm using 这是我正在使用的angular2的服务代码

export class DataService {
    // URL to web api

    constructor(private http: Http, private _configuration: Configuration,private team:Team) {

        //this.actionUrl = _configuration.ServerWithApiUrl;
        this.actionUrl = "http://localhost:8080/team";

    }
    private actionUrl: string;

    addData(postData: Team): Observable<Team[]> {

        //let body = JSON.stringify({ Team });
        this.team = postData;
        console.log(this.team);
        let body = JSON.stringify({ postData });
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });

        console.log(body);

        return this.http.post(this.actionUrl, body, options)
            .map(this.extractData)
            .catch(this.handleError);

    }

UPDATED: 更新:

For your new error message: Angular2 is lower-casing the headers. 对于您的新错误消息:Angular2使用较低的标题框。 Please update your backend to accept content-type too. 请也更新您的后端以接受content-type

res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, content-type, Accept");

You cant test post's to this URL: http://posttestserver.com/post.php?dir=anyNameYouWillFind 您无法测试此URL的帖子: http : //posttestserver.com/post.php?dir=anyNameYouWillFind

And can see your posts there: http://posttestserver.com/data/ 并可以在此处查看您的帖子: http : //posttestserver.com/data/

Browse to year, month, day and anyNameYouWillFind .. 浏览到年,月,日和anyNameYouWillFind ..

OLD: 旧:

you have to prefix your url!! 你必须给你的网址加上前缀!

this.http.get(' http ://localhost:8080/v1_user/45646/team'); this.http.get(' http :// localhost:8080 / v1_user / 45646 / team');

Fixed it by adding this to the backend node.js 通过将其添加到后端node.js来修复它

// access control --> allowed all origin
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    next();
})

.options('*', function(req, res, next){
    res.end();
})
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AngularJS-跨组织请求错误 - AngularJS - Cross Orgin Requests error SharePoint加载项[Office 365中的SharePoint托管应用程序] Rest api调用跨源资源共享错误? - SharePoint add-in [SharePoint hosted app in Office 365] Rest api call Cross Origin Resource Sharing error? Ajax调用给我跨域错误 - Ajax call giving me cross domain error Chrome应用-使用GAE频道javascript API调用channel.open()时出错 - Chrome app - error when call channel.open() using GAE channel javascript API 带有自建API的跨域错误 - Cross Domain Error with self built API Cross-Origin请求被阻止,angularjs休息调用jersey api - Cross-Origin Request Blocked, angularjs rest call to jersey api Google登录api错误 - Google login api error 如果我从 Angularjs SPA 调用 VSO API,为什么我会在 Chrome 中收到 Internet Explorer 增强的安全错误消息? - Why I get Internet Explorer enhanced security error message in Chrome if I call VSO API from Angularjs SPA? 仅在Google Chrome中,angularjs中的“排序不是函数”错误 - “sort not a function” error in angularjs only in google chrome AngularJS错误跨源请求仅支持以下协议方案:http,数据,chrome,chrome扩展名,https,chrome-extension-resource - AngularJS error Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM