将JS变量发送到特定的Symfony路由


Send a JS variable to a specific Symfony route

我有一个Symfony 3项目,前面有Angular JS。该项目是关于获取一个具有特定XML内容的URL,将其发送到SF3,进行一些操作,并将最终结果发送到Angular JS。

我只想在我的Symfony控制器中捕获我的url变量。

app.js

var app = angular.module("test", []);

app.controller("testCtrl", ['$scope', '$http', '$window', function($scope, $http, $window) {

    $scope.getMyXML = function() {

        $http.get($scope.urlSubmitted)
            .success(function(data, status, headers, config) {

                $http({
                    method: 'POST',
                    url: Routing.generate('f_api'), //FOSJsRoutingBundle
                    headers: {},
                    params: {},
                    data: {urlToParse:$scope.urlSubmitted}
                }).then(function successCallback(response) {
                    console.log('Sended to SF3');
                }, function errorCallback(response) {
                    console.log('NOT sended to sf2');
                });

            })
            .error(function(data, status, headers, config) {
                alert('!! XML GET ERROR !!');
            })
    }
}]);

Symfony控制器动作

类MyController扩展Controller{公共函数indexAction(请求$Request){

    $serializer = $this->get('jms_serializer');
    $data = $request->request->get('urlToParse');
    //var_dump($data);
    //die;


    return $this->render('MyBundle:My:index.html.twig');
}
public function apiAction(Request $request)
{
    $data = $request->request->get('urlToParse');
    var_dump($data);
    die;
    return new JsonResponse();
}

}

最后,我的路线。yml

    f_home:
    path:     /
    defaults: { _controller: MyBundle:My:index }
f_api:
    path:     /api/
    defaults: { _controller: MyBundle:My:api }
    options:
            expose: true

apiAction从未被切开,尽管上面有棱角分明的http帖子…

谢谢!

试试这个:

data: {'urlToParse':$scope.urlSubmitted}

然后:

$data = $request->request->get('urlToParse');