Conver to Ajax to curl -> curl localhost:8080/oauth/token -h foo:bar -d grant_type=password -d us



我想将其转换为curl。

curl localhost:8080/oauth/token -h foo:bar -d grant_type = password -d username = test@gmail.com -d密码= 1

上述命令成功。

但是Ajax失败...

$.ajax({
    url: 'http://localhost:8080/oauth/token',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    beforeSend: function (xhr) {
        xhr.setRequestHeader('Authorization', 'Basic' + btoa('foo:bar'));
    },
    data: {
        grant_type: 'password',
        username: 'test@gmail.com',
        password: '1'
    }, 
    complete: function(result) {
        console.log('complete', result);
    },
    success: function(result) {
        console.log('success', result);
    },
    error: function(result) {
        console.log('error', result);
    }
});

我转换为ajax并出现以下错误。

前飞行的响应无效HTTP状态代码401

Responsejson:未定义

有什么问题?

我找到了解决方案。

修改Oauthserver

更改" source.registercorsconfiguration("/**",configautenticacao);"到" source.registercorsconfiguration("/oauth/token",configautenticacao);";在CorsFilterRegistrationBean

最新更新