我是angularjs的新手。我开发了一个jax-rs restapi应用程序,它使用过滤器接受或拒绝基本身份验证调用 我已经在邮递员中测试了我的应用程序,它运行正常。如果我没有通过用户/传递进行基本身份验证,或者当用户/传递正确时一切正常时,我收到错误消息。我写了一个angularjs服务,调用$ http服务传入标头user / pass。如果用户/传递正常,则呼叫响应正确的数据,而如果用户/传递错误,我收到此错误消息
SyntaxError: Unexpected token u in JSON at position 0
而不是我在过滤器中写的自定义
Response unathourizedStatus = Response
.status(Response.Status.UNAUTHORIZED)
.entity("user cannot access the resource")
.build();
requestContext.abortWith(unathourizedStatus);
这是我的角色服务
app.service("GeafimServiceBasicAuth", function($http, $base64) {
var self = this;
// Read all Registry Type
self.getRegistryTypeBA = function(username, password) {
console.log("service user " + username);
console.log("service pass " + password);
var auth = $base64.encode(username + ":" + password);
// var headers = {"Authorization": "Basic " + auth};
console.log("service auth " + auth);
var promise = $http({
method : 'GET',
url : "webapi/basicauth/registrytype",
headers : {
"Authorization" : "Basic " + auth
}
}).then(function(response) {
return response.data;
});
return promise;
}
这是我调用服务的控制器
GeafimCtrlViewBA.$inject = [ "$http", "GeafimServiceBasicAuth" ];
function GeafimCtrlViewBA($http, GeafimServiceBasicAuth) {
var vm = this;
vm.error = false;
vm.viewRegistryType = {};
vm.getRegTypeBA = function(username, password) {
// Get registry data using a Service with Basic Auth
console.log("user " + username);
console.log("pass " + password);
GeafimServiceBasicAuth.getRegistryTypeBA(username, password).then(
function(data) {
vm.viewRegistryType = data;
}, function(err) {
console.log("error " + err)
});
}
当我写console.log时,我会在实体中写入消息,而不是语法错误
我该怎么做?
感谢
答案 0 :(得分:1)
$http
服务正在尝试JSON.parse
邮件并收到错误消息。而是将类型指定为"text/plain"
Response unathourizedStatus = Response
.status(Response.Status.UNAUTHORIZED)
.type("text/plain")
.entity("user cannot access the resource")
.build();
requestContext.abortWith(unathourizedStatus);