![](/img/trans.png)
[英]Parsing JSON data in RAILS 3.0.6 received from HTTP POST with content-type application/x-www-form-urlencoded
[英]Ember-Simple-Auth encoding request header application/x-www-form-urlencoded, Rails server expects application/json
我們正在使用Ember 2.2.3前端和背面的Rails 4.2.5和Postgres 5.7構建用於學校項目的WebApp。 我們在驗證時遇到問題,無法找到相關的解決方案。 我們認為ember應該將我們的電子郵件和密碼編碼為JSON以進行身份驗證,並且Rails希望使用JSON對象進行驗證並返回令牌。 但是實際發生的是Ember正在發送電子郵件和密碼的application / x-www-form-urlencoded版本,因此Rails會拋出415不支持的媒體類型錯誤。 我們按照本教程進行身份驗證:
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
在您的自定義身份驗證器中覆蓋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);
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.