は私がAngularJSクライアント経由で機能を使用するsymfonyにおいて、サーバーを持っているangularjs。この関数は、データ配列をパラメータとして要求します。そのタイプの :私は、サーバーをテストする際の郵便配達で得配列は
[nom => test, provenance => test, numero => 12 ]
コード:
var form = new FormData();
form.append("form[nom]", "test");
form.append("form[provenance]", "test");
form.append("form[numero]", "12");
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:8000/api/produits",
"method": "POST",
"headers": {
"authorization": "ey.....YMpJJA",
"accept": "application/json",
"cache-control": "no-cache",
"postman-token": "0dd5a3...ad03a6d3"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function(response) {
console.log(response);
});
私は今が、AngularJSで自分のアプリケーションの側で同じ結果を再現しようとすると、 HTMLのフォームを経由して自分のデータを送信するが、問題は、私は、フォームに入力された私のデータを取得することができないということです。
サーバ側:
/**
* @Route ("/api/produits", name="api_produit_add")
* @Method("POST")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function addAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$produit = $this->getProduit();
$form = $this->createFormBuilder($benevole, array('csrf_protection' => false))
->add('nom', TextType::class, array('required' => true))
->add('provenance', TextType::class, array('required' => true))
->add('numero', TextType::class, array('required' => true))
->getForm();
if ($request->isMethod('POST')) {
$form->submit($request->request->get($form->getName()));
if ($form->isValid() && sizeof($request->request->get('ingroupe'))) {
$produit->setPass(sha1($form->getData()->getPass()));
$em = $this->getDoctrine()->getManager()->flush();
return new JsonResponse('ok');
} else {
$errors = [];
foreach ($form->getErrors(true, true) as $error) {
$errors[] = $error->getOrigin()->getName() . ' : ' . $error->getMessage();
}
$response = new JsonResponse($errors);
$response->setStatusCode(Response::HTTP_NOT_ACCEPTABLE);
return $response;
}
}
$response = new JsonResponse('only POST method');
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
return $response;
}
瞬間のために、これは私が自分のアプリケーションの側に何ができるかです:
var add = function(form){
var token = window.localStorage.getItem('token');
var formdata = new FormData();
for (var key in form) {
console.log(key, form[key]);
formdata.append(key, form[key]);
}
$ionicLoading.show();
return $http({
method : 'POST',
url : API_ENDPOINT.url + '/produits',
data : formdata,
transformRequest: angular.indentity,
headers : {Authorization : 'Bearer ' + token,'Content-Type': undefined, 'Accept': 'application/json' },
}).then(function(result) {
console.dir(result.data);
},function errorCallback(response) {
console.log(response);
});
};
とコントローラで:だから
$scope.add = function() {
console.log($scope.form);
LoginService.edit($scope.form).then(function(response){
//success callback
console.log(response);
//do something with the data
},function(response){
//error callback
});
};
私はサーバから要求されたフォーマットで送信するものを正確に変換する方法を知りました。 コンテンツディスポジション:要求ペイロードで私にこれを返す
フォームデータ;名前=「NOM」
しかし、私が探している形式は次のとおりです。 コンテンツディスポジション:フォームデータ;私は、誰かが私を助けることができるideas..siのコース上だ名=「フォーム[NOM]」 はあなたがこれを行う必要があります探している結果を得るには
なぜこの質問は「angularjs」とタグ付けされていますか?ここに角コードはありません。 – georgeawg
サーバー上のデータのために何を得ていますか?あなたはそのデータを取得するために何を使用していますか? Dそれは動作します:あなたが質問が編集されたその情報 – Chausser
であなたの質問を更新することができ、私はどうもありがとうより多くの情報 – AlexisCraig