我有一个带有http get的角度js控制器。我需要得到http的响应值。在我发送的标题中:
-H "Content-Type: application/json" -H "Authorization: Basic YGDJSDJH4564363YTESNSBS4="
回复是一个简单的文字:例如。 ' VA'
我的代码:
$http({
method: 'GET',
url: 'some_url',
headers: {'Authorization': 'Basic '+tenant_auth }
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
$scope.page_error = response;
$timeout(function () {
$scope.page_error = '';
}, 10000);
});

请求返回ok,但错误是当我尝试打印响应时。
angular.min.js:117 SyntaxError: Unexpected token v in JSON at position 0
at Object.parse (native)
at uc (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:17:36)
at ac (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:91:229)
at https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:92:143
at q (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:7:355)
at fd (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:92:125)
at c (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:93:373)
at https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:130:226
at n.$eval (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:144:467)
at n.$digest (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:142:47)
答案 0 :(得分:0)
由于内容类型为application/json
,因此会尝试解析它。请尝试发送text/plain
。
答案 1 :(得分:-1)
除了其他答案,您始终可以使用$.ajax
或jQuery
在角度中,默认值为JSON
。在jQuery中,默认值为text
$.ajax({
url: 'some_url',
headers: {'Authorization': 'Basic '+tenant_auth }
}).then(function successCallback(data) {
console.log(data);
$scope.$apply() // Do not forgot to do this every time you change scope outside of angular functions
}, function errorCallback(data) {
$scope.page_error = data;
$timeout(function () {
$scope.page_error = '';
$scope.$apply() // Do not forgot to do this when you use jQuery ajax.
}, 10000);
$scope.$apply()
});