2016-10-29 6 views
0

私はSymfony 3の初心者です。SymfonyでPOSTデータを受信

投稿データの受信に問題があります。

General: 
Request URL:http://service-user.local/app_dev.php/test 
Request Method:POST 
Status Code:400 Bad Request 
Remote Address:127.0.0.1:80 

Response Headers 
view source 
Cache-Control:no-cache 
Connection:keep-alive 
Content-Type:application/json 
Date:Sat, 29 Oct 2016 06:06:05 GMT 
Server:nginx/1.11.5 
Transfer-Encoding:chunked 
X-Debug-Token:1021bb 
X-Debug-Token-Link:http://service-user.local/app_dev.php/_profiler/1021bb 
X-Powered-By:PHP/7.0.11 

Request Headers 
view source 
Accept:*/* 
Accept-Encoding:gzip, deflate, lzma 
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4 
Connection:keep-alive 
Content-Length:9 
Content-Type:text/plain;charset=UTF-8 
Host:service-user.local 
Origin:chrome-extension://kajfghlhfkcocafkcjlajldicbikpgnp 
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 OPR/40.0.2308.81 

Request Payload 
test=test 

は私が応答を取得:

use FOS\RestBundle\Request\ParamFetcher; 
use FOS\RestBundle\Controller\Annotations\Post; 
use FOS\RestBundle\Controller\Annotations\RequestParam 

.... 

/** 
* @Post("/test") 
* @RequestParam(name="test") 
*/ 
public function testAction(ParamFetcher $paramFetcher) 
{ 
    var_dump($paramFetcher->get('test')); 
    var_dump('the end'); 
} 

私は要求を送信し(私は開発者向けのクロームツールからそれを-貼り付けコピー)し:

私のコントローラは、「テスト」アクションが含まれています。

{"error":{"code":400,"message":"Bad Request","exception":[{"message":"Parameter \"test\" of value \"NULL\" violated a constraint \"This value should not be null.\"","class":"FOS\\RestBundle\\Exception\\InvalidParameterException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"\/code\/vendor\/friendsofsymfony\/rest-bundle\/Exception\/InvalidParameterException.php","line":68,"args":[]},{"namespace":"FOS\\RestBundle\\Exception","short_class":"InvalidParameterException","class":"FOS\\RestBundle\\Exception\\InvalidParameterException","type":"::","function":"withViolationsAndMessage","file":"\/code\/vendor\/friendsofsymfony\/rest-bundle\/Exception\/InvalidParameterException.php","line":52,"args":[["object","FOS\\RestBundle\\Controller\\Annotations\\RequestParam"],["object","Symfony\\Component\\Validator\\ConstraintViolationList"],["string","Parameter \"test\" of value \"NULL\" violated a constraint \"This value should not be null.\""]]},{"namespace":"FOS\\RestBundle\\Exception","short_class":"InvalidParameterException","class":"FOS\\RestBundle\\Exception\\InvalidParameterException","type":"::","function":"withViolations","file":"\/code\/vendor\/friendsofsymfony\/rest-bundle\/Request\/ParamFetcher.php","line":162,"args":[["object","FOS\\RestBundle\\Controller\\Annotations\\RequestParam"],["object","Symfony\\Component\\Validator\\ConstraintViolationList"]]},{"namespace":"FOS\\RestBundle\\Request","short_class":"ParamFetcher","class":"FOS\\RestBundle\\Request\\ParamFetcher","type":"->","function":"cleanParamWithRequirements","file":"\/code\/vendor\/friendsofsymfony\/rest-bundle\/Request\/ParamFetcher.php","line":108,"args":[["object","FOS\\RestBundle\\Controller\\Annotations\\RequestParam"],["null",null],["boolean",true]]},{"namespace":"FOS\\RestBundle\\Request","short_class":"ParamFetcher","class":"FOS\\RestBundle\\Request\\ParamFetcher","type":"->","function":"get","file":"\/code\/src\/AppBundle\/Controller\/DefaultController.php","line":24,"args":[["string","test"]]},{"namespace":"AppBundle\\Controller","short_class":"DefaultController","class":"AppBundle\\Controller\\DefaultController","type":"->","function":"testAction","file":null,"line":null,"args":[["object","FOS\\RestBundle\\Request\\ParamFetcher"]]},{"namespace":"","short_class":"","class":"","type":"","function":"call_user_func_array","file":"\/code\/vendor\/symfony\/symfony\/src\/Symfony\/Component\/HttpKernel\/HttpKernel.php","line":153,"args":[["array",[["object","AppBundle\\Controller\\DefaultController"],["string","testAction"]]],["array",[["object","FOS\\RestBundle\\Request\\ParamFetcher"]]]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"HttpKernel","class":"Symfony\\Component\\HttpKernel\\HttpKernel","type":"->","function":"handleRaw","file":"\/code\/vendor\/symfony\/symfony\/src\/Symfony\/Component\/HttpKernel\/HttpKernel.php","line":68,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["string","1"]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"HttpKernel","class":"Symfony\\Component\\HttpKernel\\HttpKernel","type":"->","function":"handle","file":"\/code\/vendor\/symfony\/symfony\/src\/Symfony\/Component\/HttpKernel\/Kernel.php","line":169,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["string","1"],["boolean",true]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"Kernel","class":"Symfony\\Component\\HttpKernel\\Kernel","type":"->","function":"handle","file":"\/code\/web\/app_dev.php","line":30,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"]]}]}]}} 

何が問題ですか?何か設定する必要がありますか?

答えて

1

ペイロードデータが適切なJSONではないようですか?

{ "foo" : "bar", "name" : "John" } 

jqueryでデータを送信する場合は、フォーム上で.serialize()関数を使用します。それはあなたのパラメータを好きではない場合、それはあなたが何であるかである400エラーを返しますhttp://symfony.com/doc/current/bundles/FOSRestBundle/param_fetcher_listener.htmlのドキュメントによると

{ "test" : "test" } 

:あなたは、単一の変数としてデータを送信する場合、単純にこのようなメッセージを定義します取得。

+0

ありがとう、とても助かりました! – Simon

関連する問題