0
我有Angularjs應用程序連接到服務器使用API,我使用令牌認證,當我使用郵遞員獲取令牌時,它的作品完美,但是當我使用Angulajs與相同的標題和參數我得到錯誤:400。Angularjs訪問控制允許來源
當我使用Fiddler檢查了兩個請求時,發現Angularjs的請求缺少Access-Control-Allow-Origin: *
標題。
如何解決這個問題?
這裏是用於獲取令牌服務:爲API服務器
AuthenticationApi.Login = function (loginData) {
//POST's Object
var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;
var deferred = $q.defer();
//the data will be sent the data as string not JSON object.
$http.post('http://localhost:53194/Token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.then(function (response) {
console.log(response);
localStorageService.set('authorizationData',
{
token: response.access_token,
userName: loginData.userName
});
Authentication.isAuth = true;
Authentication.userName = loginData.userName;
console.log(Authentication);
deferred.resolve(response);
},
function (err, status) {
logout();
deferred.reject(err);
});
return deferred.promise;
};
,我心中已經完成CORS:
public void Configuration(IAppBuilder app)
{
ConfigureOAuth(app);
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
使CORS是100%的服務器端的問題。你只是用角度標記了這個,但沒有與你的服務器端語言或服務器類型有關 – charlietfl