2016-10-29 77 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'); 
} 

當我發送一個請求(我的Chrome工具,開發人員抄粘貼它):

{"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()函數的表單上的數據。在http://symfony.com/doc/current/bundles/FOSRestBundle/param_fetcher_listener.html

{ "test" : "test" } 

根據該文件,如果它不喜歡你的參數,它會返回一個400錯誤,這是你是什麼:如果您提交的數據作爲一個變量簡單的定義是這樣的消息獲得。

+0

謝謝,這是非常有益的! – Simon