Last active
August 29, 2015 14:24
-
-
Save zburgermeiszter/d837e7c62db5e338a596 to your computer and use it in GitHub Desktop.
Sending empty arrays with jquery to Symfony
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.ajax({ | |
| url:postURL, | |
| type:"POST", | |
| data:JSON.stringify(data), | |
| contentType:"application/json; charset=utf-8", | |
| dataType:"json" | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public function yourAction() | |
| { | |
| $requestContent = $request->getContent(); | |
| if ( | |
| 0 !== strpos($request->headers->get('Content-Type'), 'application/json') || | |
| empty($requestContent) | |
| ) { | |
| throw new \Exception("Invalid request format."); | |
| } | |
| $config = json_decode($requestContent, true); | |
| $config = is_array($config) ? $config : array(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http://silex.sensiolabs.org/doc/cookbook/json_request_body.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment