2014-07-21 34 views
0

我在使用以下方法使用angularjs 調用webservice時出現以下錯誤。Webservice調用期間Angularjs中的異常

$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 

問題是什麼?我該如何解決它?

感謝和問候, 喬治

+1

這意味着JSON結果是錯誤的,是有語法問題與JSON –

+0

我收到以下錯誤:未能執行'XMLHttpRequest'上的'setRequestHeader':''不是有效的HTTP頭字段值。 –

+0

我認爲你需要刪除ID,也許這個令牌只是離開內容類型 –

回答

1

你需要改變你的要求,刪除ID和從頭部的道理,只留下內容類型,如果你想送cradentials,您可以添加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} - whether to set the withCredentials flag on the XHR object. See [requests with credentials] https://developer.mozilla.org/en/http_access_control#section_5