2017-04-07 127 views
2

嗨,大家好我是symfony 2中的新成員,我對symfony 2中的php控制器發送ajax數據感到困惑。我想用google map創建項目,所以我創建MapController:在symfony2中發送Ajax數據到php控制器

<?php 

namespace AppBundle\Controller; 

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\JsonResponse; 
class MapController extends Controller 
{ 
    /** 
    * @Route("/", name="homepage") 
    */ 
    public function indexAction(Request $request) 
    { 
     // replace this example code with whatever you need 
     return $this->render('map/map.html.twig', [ 
      'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR, 
      'contData' => ['lat' => '51.24591334500776', 'lng'=> '22.56967306137085'] 
     ]); 
    } 


    public function saveAction(Request $request) 
    { 
     $data = $request->request->get('params'); 

     return new JsonResponse(['data' => $data], Response::HTTP_OK); 
    } 

} 

然後創建路由:

app: 
    resource: '@AppBundle/Controller/' 
    type: annotation 
map: 
    path:  /map 
    defaults: { _controller: AppBundle:Map:index } 
    requirements: 
     page: '\d+' 
map_save: 
    path:  /map/save 
    defaults: { _controller: AppBundle:Map:save } 
    methods: [POST] 

所以,當我去到路線:

http://localhost/googlemap/web/app_dev.php/map

我顯示我的模板map.html.twig

有我有SCRIPT關閉在那裏我試圖ajax的數據發送到控制器的一些:

marker.addListener("click", function() { 


        //! 
        var http = new XMLHttpRequest(); 
        var url = "map/save"; 
        var params = marker.position.lat(); 
        http.open("POST", url, true); 

//Send the proper header information along with the request 
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

        http.onreadystatechange = function() {//Call a function when the state changes. 
         if (http.readyState == 4 && http.status == 200) { 
          alert(http.responseText); 
         } 
        } 
        http.send(params); 

但是我在這個響應NULL: {「data」:null}

回答

1

你需要問自己,你想用JS發送的這個數據做什麼。如果它與地圖特徵有關,那麼在MapController中創建一個新的方法看起來很好。如果它與地圖無關,那麼創建一個新的控制器可能是一個好方法。

方法和控制器的命名應該與你正在做的事情相關。您的saveData示例並不那麼明顯。如果您要保存座標,那麼您可以將此方法命名爲saveCoordinatesAction並定義僅支持POST請求的專用路由。

至於將URL傳遞給JS,請查看FOSJsRoutingBundle - 它允許您直接從JavaScript生成特定路由。

+0

我知道,但這個函數的名字只是現在測試。所以當我在我的MapController中創建一個saveCoordinatesAction方法時,應該如何在javascript中查看我的var url?我試圖用「map/saveCoordinatesAction」,「map/saveCoordinates」,「app_dev.php/map/saveCoordinatesAction」等組合,但是這個請求stil沒有達到,沒有來到我的php函數在這個控制器中。 – johnyT

+0

mam POST http ://localhost/googlemap/web/app_dev.php/saveCoordinatesAction 404(Not Found),szczerze jestem nowy w symfony kilka godzin temupostawiłemprojekt,żebysięnauczyći te rutingi mnie jesczetrochęmylą,wcześniejużywałemnp Yii2 framework,gdzie w ogole nie trzebasięmartwićo takei sprawy – johnyT

+0

如果你的基地址是'http:// localhost/googlemap/web/app_dev.php /',那麼你應該使用所有的這個(包括'http://'協議前綴)在你的javascript調用的URL中,例如'http:// localhost/googlemap/web/app_dev.php/saveCoordinates'('app_dev.php'是可選的,把它留在你的生產服務器上),但是你需要在'routing.yml'文件中爲這個地址定義一個路由,就像你爲'map' URL所做的一樣。 – Najki