【问题标题】:Angularjs - Response to preflight request doesn't pass access controlAngularjs - 对预检请求的响应未通过访问控制
【发布时间】:2018-04-14 22:44:12
【问题描述】:

我有以下 angularjs 代码:

    this.createAccount = function (payload) {

    var parameters = {
        grant_type: 'password',
        client_id: clientId,
        client_secret: clientSecret,
        email: payload.Email,
        password: payload.Password,
        scope: 'myApi offline_access openid'
    }
    return $http({
        method: "POST",
        url: $sce.trustAsResourceUrl(identityApiURL + '/account/register'),
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
        },
        data: 'grant_type=' + parameters.grant_type + '&client_id=' + parameters.client_id + '&client_secret=' + parameters.client_secret + '&email=' + encodeURIComponent(parameters.email) + '&password=' + parameters.password + '&scope=' + parameters.scope
    }).error(function (response) {
        $log.error('Something went wrong when creating your account', response);
    });

}

在尝试运行它时,我不断在控制台中收到以下错误:

无法加载https://........account/register:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'https://localhost:3000' 因此不允许访问。

我可以看到正在使用我的参数创建一个配置对象,但数据为 NULL。 同样在网络上,我看到了 OPTIONS,但没有触发 POST 方法...

怎么了?

提前致谢!

【问题讨论】:

标签: angularjs http post cors


【解决方案1】:

原来我有: $http.defaults.headers.common['ApiKey'] 设置默认添加到标头中,并且在我的标头对象之前用于 POST 请求。 一旦我删除它就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 2016-06-05
    • 2017-05-19
    相关资源
    最近更新 更多