【问题标题】:Angular 2 HttpErrorResponse during parsing the response解析响应期间的Angular 2 HttpErrorResponse
【发布时间】:2018-06-02 22:22:18
【问题描述】:

http get请求返回和xsd文件内容,下面是代码sn -p

headers = headers.append('Content-Type', 'text/plain');
        headers = headers.append('responseType', 'text');
        headers = headers.append('Accept', 'application/octet-stream');

retun this.http.get(data[0]._links['http://identifiers.emc.com/content-download'].href, { headers: headers }))
      .map((response: Response) => {
        let data: any;
        data = response;
      return content;
      })

代码失败并出现错误

“语法错误:JSON.parse 中位置 0 处的 JSON 中的意外标记

我已将“requestType:text”添加到标题中,我是否遗漏了什么,为什么它试图解析认为响应是 JSON 的?

以下是请求和响应标头的快照

Response header Request header

以下是响应示例

 <!--
      ~ Copyright (c) OpenText Corporation. All Rights Reserved.
      -->
    <xs:schema elementFormDefault="qualified" targetNamespace="urn:eas-samples:en:xsd:phonecalls.1.0" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="Calls">
            <xs:complexType>
                <xs:sequence maxOccurs="unbounded">
                    <xs:element name="Call">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="SentToArchiveDate" type="xs:date"/>
                                <xs:element name="CallStartDate" type="xs:dateTime"/>
                                <xs:element name="CallEndDate" type="xs:dateTime"/>
                                <xs:element name="CallFromPhoneNumber">
                                    <xs:simpleType>
                                        <xs:restriction base="xs:positiveInteger">
                                            <xs:minInclusive value="1"/>
                                            <xs:totalDigits value="11"/>
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:element

【问题讨论】:

标签: angular


【解决方案1】:

你必须设置responseType,因为默认的是json,你只是指定了请求头: 查看 HttpRequest 文档:https://angular.io/api/common/http/HttpRequest 响应类型可以是

'数组缓冲区' | '斑点' | 'json' | '文本'

我想你会需要这样的东西:

this.http.get(data[0]._links['http://identifiers.emc.com/content-download'].href, { headers: headers, responseType: 'text' }))
  .map((response: Response) => {
    let data: any;
    data = response;
  return content;
  })

【讨论】:

  • 谢谢,这有帮助,我认为添加标题就足够了
【解决方案2】:

我认为您可以使用模块来解析您的 xml 响应或使用将 xml 转换为 json 的模块。

您可以使用以下 2 个模块:

【讨论】:

    【解决方案3】:

    我也有类似的问题,对我来说,解决方案是在角度和 api 端处理响应类型。

    角度

    return this.http.post(this.resourceUrl+'/imageUpload', copy, {responseType: 
    'text'})
    

    Java

    @PostMapping(value = "/questions/imageUpload",produces = { "text/plain" })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-23
      • 2017-12-04
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 2019-05-13
      相关资源
      最近更新 更多