目的: 我(一个javascript初学者)我正在尝试对Web服务器(Splunk)执行Rest API调用,获取内容类型为json的数据。调用由基于angularjs的webApp启动,有问题的代码片段发布在下面。使用firebug和响应& amp;标题信息如下所述。
Angular Code Snippet :
$scope.onClickButton = function (button) {
$http({
method: 'POST',
url: 'https://localhost:8089/servicesNS/admin/search/search/jobs/export',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Splunk ' + $scope.sessionKey
},
data: {search: button.query, output_mode: 'json'}
}).success(function (response) {
alert("Success");
}).error(function (response) {
alert("error");// alerted error
alert(response); // alerts 'undefined'
});
};
响应对象(由firebug捕获)
{"预览":假,"偏移":0,"导致" {" _serial":" 0& #34;," _sourcetype":" mylogger"," splunk_server":" mohit-PC"}} {"预览":假,"偏移":1,"导致" {" _serial":" 1" ," _sourcetype":" mylogger"," splunk_server":" mohit-PC"}} {"预览":假,"偏移":2"导致" {" _serial":" 2" ," _sourcetype":" mylogger"," splunk_server":" mohit-PC"}} ...
响应标题
Access-Control-Allow-Head... Authorization
Access-Control-Allow-Meth... GET,POST,PUT,DELETE,HEAD,OPTIONS
Access-Control-Allow-Orig... *
Cache-Control no-store, no-cache, must-revalidate, max-age=0
Connection Keep-Alive
Content-Type application/json; charset=UTF-8
Date Tue, 02 Dec 2014 13:00:34 GMT
Expires Thu, 26 Oct 1978 00:00:00 GMT
Server Splunkd
Transfer-Encoding chunked
Vary Authorization
access-control-allow-cred... true
x-content-type-options nosniff
x-frame-options SAMEORIGIN
Firebug错误
错误:JSON.parse:来自JSON数据的第2行第1列的JSON数据之后的意外非空白字符来自于Json @angular.js:1054:9
问题
Q1。响应的状态是 - 200 OK
,那么为什么会抛出错误?虽然很明显响应文本不代表json对象或json对象数组。 []和逗号不存在。无法将服务器配置为使用正确的格式化数据进行响应。似乎angular执行格式验证并抛出异常。可以避免/绕过此验证吗?
Q2。即使发生了错误,为什么响应对象未定义'在http.error方法?如果不是,我可以将字符串操作作为一种解决方法。