【发布时间】:2016-06-23 11:13:52
【问题描述】:
我使用ember-simple-auth 和ember-simple-auth-token 来允许用户登录我的应用程序。但是,当我使用 POST 请求在后端调用 Django Rest Framework 以使用用户名和密码进行身份验证时,我收到 406(不可接受)错误。这在 DRF 可浏览 API 中不会发生,因此后端似乎工作正常。
我怀疑与 CORS 相关的事情。我在 Django 中使用 django-cors-headers,并在我的开发环境中允许所有内容。我还使用django-rest-framework-jwt 和django-rest-framework-json-api 包,如果这很重要的话。
我的 API 显示一个 OPTIONS,然后是一个 POST 调用:
[09/Mar/2016 07:15:54] "OPTIONS /api-token-auth/ HTTP/1.1" 200 0
[09/Mar/2016 07:15:54] "POST /api-token-auth/ HTTP/1.1" 406 114
响应标头:
HTTP/1.0 406 Not Acceptable
Date: Wed, 09 Mar 2016 07:15:54 GMT
Server: WSGIServer/0.2 CPython/3.5.1
X-Frame-Options: SAMEORIGIN
Access-Control-Allow-Origin: *
Content-Type: application/vnd.api+json
Allow: POST, OPTIONS
Vary: Accept
请求标头:
POST /api-token-auth/ HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Content-Length: 2
Accept: application/json, text/javascript
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/48.0.2564.116 Chrome/48.0.2564.116 Safari/537.36
Content-Type: application/json
Referer: http://localhost:4200/login
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
请求标头不显示application/vnd.api+json,而是显示application/json。不幸的是,无论我在 Ember 中做什么,都能解决这个问题。我尝试将我应用的 JSONAPIAdapter 和 ENV['ember-simple-auth-token'] 的标头设置为 "Accept": "application/vnd.api+json",但未成功。
【问题讨论】:
-
听说ember和DRF默认是不兼容的。你在用github.com/dustinfarris/ember-django-adapter之类的东西吗?
-
@ilse2005,我正在使用
django-rest-framework-json-api使 DRF 响应 JSON API 规范兼容。
标签: ember.js django-rest-framework ember-simple-auth django-cors-headers