Javascript 将ajax数据发送到symfony2中的php控制器
大家好,我是symfony2的新手,我对使用ajax将数据发送到symfony2中的php控制器有点困惑。我想使用google map创建项目,因此我创建了MapController:Javascript 将ajax数据发送到symfony2中的php控制器,javascript,php,ajax,symfony,Javascript,Php,Ajax,Symfony,大家好,我是symfony2的新手,我对使用ajax将数据发送到symfony2中的php控制器有点困惑。我想使用google map创建项目,因此我创建了MapController: <?php namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Contr
<?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);
}
}
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);
所以当我去路线:
我显示我的模板map.html.twig
这里有一个javascipt函数,我试图将ajax数据发送到某个控制器:
<?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);
}
}
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);
但我在这个回答中没有空值:
{“data”:null}你需要问问自己,你想对JS发送的数据做什么。若它和地图特性有关,那个么在MapController中创建一个新方法似乎是不错的。如果它与地图无关,那么创建一个新的控制器可能是一个好方法 方法和控制器的命名应该与您正在做的事情相关。您的
saveData
示例并不明显。如果要保存坐标,则可以将此方法命名为saveCoordinateAction
,并定义仅支持POST请求的专用路由
至于将URL传递给JS,请检查-它允许您直接从JavaScript生成特定的路由。我知道,但这个函数名现在仅用于测试。所以,当我在MapController中创建一个方法saveCoordinateAction时,我应该如何在javascript中查看我的var url?我尝试使用“map/saveCoordinationAction”、“map/saveCoordinations”、“app_dev.php/map/saveCoordinationAction”和其他组合,但这个请求仍然没有达到,也没有在这个ControllerAM POST 404中找到我的php函数(未找到),szczerze jestem nowy w symfony kilka godzin temu postawiłem projekt,eby sięnauczyći te rutingi mnie jescze trochęmylą,wcześniej używa em np Yii2 framework,gdzie w ogole nie trzeba sięmartwićo takei Sprawyi English将更容易让其他人加入我们的讨论。如果您的基本URL是
http://localhost/googlemap/web/app_dev.php/
那么您应该在javascript调用的URL中使用所有这些(包括http://
protocol前缀),例如http://localhost/googlemap/web/app_dev.php/saveCoordinates
(app_dev.php
是可选的,只是不要把它留在生产服务器上)但是你需要在routing.yml
文件中为这个地址定义一个路由,就像你为map
URL定义路由一样。你也可以检查你的dev.log
文件,看看这个来自JS的请求是否符合你的应用程序。另外,使用php-bin/console-debug:router
CLI命令检查你的saveCoordinates
路由是否被取消正确地罚款(意味着它列在该命令的输出上)。好的,我使用$content=$request->getContent();而不是$data=$request->request->get('params');谢谢你们的帮助:))祝你们愉快:)