【问题标题】:Ember-Simple-Auth encoding request header application/x-www-form-urlencoded, Rails server expects application/jsonEmber-Simple-Auth 编码请求头 application/x-www-form-urlencoded,Rails 服务器需要 application/json
【发布时间】:2016-03-28 16:22:38
【问题描述】:

我们正在使用 Ember 2.2.3 前端、Rails 4.2.5 和 Postgres 5.7 为学校项目构建 WebApp。我们遇到了身份验证问题,找不到相关的解决方案。我们认为 ember 应该将我们的电子邮件和密码编码为 JSON 以进行身份​​验证,Rails 期望 JSON 对象用于验证并返回令牌。但实际发生的是 Ember 正在发送电子邮件和密码的 application/x-www-form-urlencoded 版本,因此 Rails 将抛出 415 Unsupported Media Type Error。我们按照本教程进行身份验证:

http://romulomachado.github.io/2015/09/28/using-ember-simple-auth-with-devise.html

Ember 中有没有办法将此编码更改为 application/json?或者我们可以更改 Rails 以接受 application/x-www-form-urlencoded?

这是我们当前的身份验证请求标头:

POST /users/sign_in HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 63
Accept: application/json, text/javascript
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:4200/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

【问题讨论】:

    标签: ruby-on-rails json authentication ember.js devise


    【解决方案1】:

    在您的自定义身份验证器中覆盖 makeRequest()

      // your-app/authenticators/your-custom-authenticator.js:
      // serverTokenEndpoint: ...
      // ...
    
      makeRequest(url, data) {
        const options = {
          url: url,
          data: data,
          type: 'GET',
          dataType: 'json',
          accept: 'application/vnd.api+json',
          headers: {
            "Content-Type": 'application/vnd.api+json'
          }
        };
    
        return Ember.$.ajax(options);
      },
    

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 2014-03-30
      • 1970-01-01
      • 2017-11-09
      • 2021-06-19
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 2016-04-13
      相关资源
      最近更新 更多