使用angularjs调用Web服务时出现以下错误
使用以下方法。
$http({
url : 'http://localhost:8080/dummy',
method : 'POST',
data : {'id':'1','name':'test'},
headers : {'id': '1',
'token': '34343ggfgf4444',
'Content-Type': 'application/json'}
})
Error: SyntaxError: DOM Exception 12
Error: An invalid or illegal string was specified.
at http:localhost:8080/lib/angular/angular.js:8054:17
at forEach (http:localhost:8080/test/lib/angular/angular.js:329:20)
at http:localhost:8080/test/lib/angular/angular.js:8052:7
at sendReq (http:localhost:8080/test/lib/angular/angular.js:7911:9)
at $http.serverRequest (http:localhost:8080/test/lib/angular/angular.js:7652:16)
at wrappedCallback (http:localhost:8080/test/lib/angular/angular.js:11042:81)
at wrappedCallback (http:localhost:8080/test/lib/angular/angular.js:11042:81)
at http:localhost:8080/test/lib/angular/angular.js:11128:26
at Scope.$eval (http:localhost:8080/test/lib/angular/angular.js:12069:28)
at Scope.$digest (http:localhost:8080/test/lib/angular/angular.js:11898:31) angular.js:9505
有什么问题我该如何解决?
谢谢并恭祝安康,
乔治
最佳答案
您需要更改请求,从标头中删除ID和令牌,仅保留内容类型,如果要发送证书,可以将withCredentials: true
添加到请求中。
$http({
url : 'http://l-o-c-a-l-h-o-s-t:8080/dummy',
method : 'POST',
data : {'id':'1','name':'test'},
headers : {'Content-Type': 'application/json'},
withCredentials: true})
您可以在这里阅读有关credntils的更多信息:
withCredentials-{boolean}-是否设置withCredentials标志
在XHR对象上。请参见[具有凭据的请求] https://developer.mozilla.org/en/http_access_control#section_5