【问题标题】:Exception in Angularjs during Webservice CallWeb服务调用期间Angularjs中的异常
【发布时间】:2014-07-21 14:28:18
【问题描述】:

使用 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

有什么问题?我该如何解决?

感谢和问候, 乔治

【问题讨论】:

  • 表示json结果错误,json有语法问题
  • 我收到以下错误:无法在“XMLHttpRequest”上执行“setRequestHeader”:“”不是有效的 HTTP 标头字段值。
  • 我认为您需要删除 id,也许令牌也只是保留内容类型
  • 感谢@Laid Livnat。如何将令牌和 Id 发送到 Webservice?如何发送数据和 Header 机密数据,如 id 和 token?

标签: javascript jquery angularjs web-services dom


【解决方案1】:

您需要更改您的请求,从 headers 中删除 id 和 token,只保留 content-type,如果您想发送凭证,您可以在请求中添加 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    相关资源
    最近更新 更多