JSONをRuby API用にシリアル化するにはどうすればいいですか?angular2のhttpParamSerializerJQLike?
アンギュラ1
$scope.submitForm = function() {
var data = {"contato": $scope.contato, "id":$scope.contato.id, "_method":'PUT'};
$http.post(
'http://myApi/contatos/' + $scope.contato.id,
**$httpParamSerializerJQLike(data)**,
{
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
datatype: "JSONP"
}).then(function successCallback(response) {
modalContato.show();
setTimeout(function(){
modalContato.hide();
$state.go('contato-detalhe', {"id":$scope.contato.id});
}, 1500);
});
};
Angular2:
insertContato(contato: Contato) {
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
let options = new RequestOptions({ headers: headers });
this._http
.post(this.urlApi + '/contatos', JSON.stringify(contato), options)
.subscribe(data => {
console.log('Funciona: ' + data.text());
}, error => {
console.log('Erro: ' + error.text())
});
}
"JSON.stringify(contato)は" それは$ httpParamSerializerJQLike(データ)と同じ動作を持っていません。 JSONの壊れたサーバ内の...
はContatosController番号:2016年4月13日午後01時25分55秒-0300 処理で127.0.0.1のために、 "/ contatos" POSTを開始作成HTML パラメータとして:{ "{\" nome \ ":\" asd \ "、\" email \ ":\" [email protected] \ "、\" telefone \ ":\" 123 \ "}" =>なし) 完了400 4msの中に不正な要求(ActiveRecordの:0.0ms)
正しいです:
はPOSTを開始 "/ contatos" のために127.0.0.1 at 2016-04-12 17:00:24 -0300 ContatosControllerによる処理#create as JSON パラメータ:{"contato" => {"nome" => "felipe"、 "telefone" => "5555 "}} 完了278msで200 OK(閲覧:0.1ms | ActiveRecord:229.4ms)