406(不可接受)铁-ajax

406 (Not Acceptable) iron-ajax

这是我的代码。

 <iron-ajax auto
                id="requestRepos"    
                url="myurl"
                params='{"mycommaseperatedparams"}'
                handle-as="json"
                on-response="handleResponse"></iron-ajax>

如果我在浏览器中手动点击 url,它就可以了。但是这个没有。 这是一个 GET 请求。

HTTP 状态代码406 表示服务器无法return 符合Accept- header 的表示。来自 specs:

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

有关更多答案,请参见此处:What is "406-Not Acceptable Response" in HTTP?

这很可能是 Accept header 被 iron-ajax 元素设置为 application/json。另一方面,浏览器 (Chrome) 默认发送带有

的请求
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

其中 */* 位匹配任何内容类型。

要修复,您必须在服务器端工作以允许 JSON 响应。您也可以尝试显式设置 header,尽管我希望 iron-ajax 覆盖 Accept header

<iron-ajax headers='{"Accept": "*/*"}' handle-as="json"></iron-ajax>

同样,*/* 只是一个例子。您可能需要更具体的媒体类型。