2015-12-07 34 views
0

我有一個Symfony 3項目,前面有Angular JS。 該項目是關於帶有特定XML內容的URL,將其發送到SF3,做一些操作,並將最終結果發送給Angular JS。發送一個JS變量到特定的Symfony路由

我只想在我的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的控制器 { 公共功能的indexAction(請求$請求) {

$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從未lanched,儘管它的角HTTP POST的...

謝謝!

回答

0

試試這個:

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

然後:

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

我有一個的indexAction(我的提交表單),以及apiAction(我想在這裏趕上網址我已經把你的代碼,當我把一個var_dump(data); die;在我的apiAction中,當我提交我的表單時,沒有任何東西出現... – Ajman

+0

你是否嘗試傳遞你的參數到url中:ex:Routing.generate('my_route_to_expose',{id :10}); –

+0

你可以編輯你的文章並添加你的apiAction的註釋 –