PolymerElements / iron-ajax

Easily make ajax requests
https://www.webcomponents.org/element/PolymerElements/iron-ajax
127 stars 113 forks source link

polymer3 handle response crash error. #332

Open dravenk opened 5 years ago

dravenk commented 5 years ago

Description

I use the Drupal jsonapi module to return json data. And then I used this module with polymer, and I got an error when I demo.

Not work. The actual result does not print out the response result

<iron-ajax 
 ...
 handle-as="json"
 on-response="handleResponse"
...
</iron-ajax>

Working. I changed it to the following to print out the content, but the prompt
iron-request.js:315 The provided value 'application/json' is not a valid enum value of type XMLHttpRequestResponseType.

<iron-ajax 
 ...
 handle-as="application/json"
 on-response="handleResponse"
...
</iron-ajax>

handle function.

  handleResponse(event) {
    let response = JSON.parse(event.detail.response);
    console.log(response);
  }

Expected outcome

Print out the response results

Actual outcome

VM34783:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at HTMLElement.handleResponse (sample-element.js:30)
    at HTMLElement.handler (template-stamp.js:90)
    at HTMLElement.fire (legacy-element-mixin.js:440)
    at HTMLElement._handleResponse (iron-ajax.js:547)

Steps to reproduce

package.json

...
  "version": "4.0.0",
...
  "repository": "Polymer/polymer-starter-kit",
...

Browsers Affected