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

时间:2015-12-07 17:10:52

标签: javascript php angularjs symfony

我在前面有一个带Angular JS的Symfony 3项目。 该项目是关于获取具有特定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 {     public function indexAction(Request $ 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();
}

}

最后,我的routing.yml

    f_home:
    path:     /
    defaults: { _controller: MyBundle:My:index }

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

尽管apiAction上有一个有棱角的http帖子,但apiAction永远不会被打嗝......

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

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

然后:

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