【发布时间】:2016-05-28 23:54:56
【问题描述】:
我是 angularjs 的新手,我正在尝试用 body 来发布资源以存在 api 当我在邮递员上发送带有正文的帖子时,它可以正常登录并取回令牌
API 工作正常 我怎么能在 angularjs 上做到这一点
这是我的代码 这是api
[Route("v1/token")]
[HttpPost]
public IHttpActionResult Post(LoginViewModel viewModel)
{}
我的资源工厂
app.factory('getToken', function ($resource, $q) {
return function (email, password) {
console.log(email);
console.log(password);
return $resource('http://localhost:9303/v1/token', {}, {
login: {
method: 'POST',
headers: {
'Content-Type': 'application/jsonp;'
},
body: {
Email: email,
Password: password
}
}
});
}
});
这是我的登录功能
loginService.login = function (username, password) {
var deferred = $q.defer();
if (username === 'erez' && password === 'pass') {
var src = getToken('email','password').login(function (result) {
console.log(result);
});
console.log(src);
deferred.resolve(src);
}
else {
deferred.reject({ message: 'Unauthorized user' });
}
return deferred.promise;
}
我得到了那个错误
POST http://localhost:9303/v1/token (anonymous function) @ angular.js:11442r @ angular.js:11235g @ angular.js:10945(anonymous function) @ angular.js:15552m.$eval @ angular.js:16820m.$digest @ angular.js:16636m.$apply @ angular.js:16928(anonymous function) @ angular.js:24551n.event.dispatch @ jquery-2.2.0.min.js:3r.handle @ jquery-2.2.0.min.js:3
(index):1 XMLHttpRequest cannot load http://localhost:9303/v1/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:53647' is therefore not allowed access. The response had HTTP status code 500.
当我对 api 进行调试时,当我到达 api 时,viewModel 为空 为什么它为空 他没有得到我的身体
我尝试用参数替换 body 但相同
感谢帮助
【问题讨论】:
-
嗯,你的问题是这个“没有'Access-Control-Allow-Origin'标头存在......”。这是您的服务器拒绝请求,因为来自它不知道的位置。需要对服务器进行更改以允许来自其他来源的请求。你应该做一些关于它的阅读
-
那么它如何与具有相同标题的邮递员一起使用?
-
Chrome 打包应用可以拥有跨域权限。当您安装 Postman 时,它会提示您此应用将访问任何域。
标签: c# angularjs asp.net-web-api2