Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将变量从jquery fetch函数传递到symfony routes.yaml?_Php_Jquery_Symfony_Routing_Symfony4 - Fatal编程技术网

Php 如何将变量从jquery fetch函数传递到symfony routes.yaml?

Php 如何将变量从jquery fetch函数传递到symfony routes.yaml?,php,jquery,symfony,routing,symfony4,Php,Jquery,Symfony,Routing,Symfony4,因此,我想在symfony中调用一个控制器,方法是使用jquery fetch函数传入routes.yaml中声明的路由,然后将变量从jquery传递给控制器。我该怎么做 这是我的jquery。我调用这个路径,我想传递上面的两个变量 var longitudde = lonLat[0]; var latudde = lonLat[1]; fetch('/weather_request) .then(function(response) { return response.json(); })

因此,我想在symfony中调用一个控制器,方法是使用jquery fetch函数传入routes.yaml中声明的路由,然后将变量从jquery传递给控制器。我该怎么做

这是我的jquery。我调用这个路径,我想传递上面的两个变量

var longitudde = lonLat[0];
var latudde = lonLat[1];
fetch('/weather_request)
.then(function(response) {
  return response.json();
}).then(function(json) {
  // ...
});
要将这些变量传递到Symfony中的routes.yaml,请执行以下操作:

weather_request:
    path:     /weather_request
    controller: App\Controller\WeatherController::weather
    methods: [get]
    defaults:
      longitude: longitude
      latitude: latitude
要在WeatherController中的天气功能中最终传递它们,请执行以下操作:

public function weather($longitude, $latitude)
{
    return $this->render('weather/index.html.twig', ['weatherInfos' => $this->weatherService->getWeather($longitude, $latitude)]);
}

那么,如何将经度和纬度从jqueryfetch传递到这里的控制器呢?我是Symfony的新手,所以我可能完全错了。

我想这可以帮助您:

  $.ajax({
  url: '{{path('weather_request')}}',
  type: 'GET',
  data: { 
  longitude: lonLat[0], 
  latutide: lonLat[1], 
  },
  success: function(response) {
    //called when successful
  },
  error: function(e) {
    //called when there is an error
    //console.log(e.message);
  }
});
在控制器中:

use Symfony\Component\HttpFoundation\Request;

public function weather(Request $request)
{
  if ($request->isXMLHttpRequest()) {         
   $longitude = $request->query->get('longitude');
   $latutide  =  $request->query->get('latutide');
 }

}
对我来说,我是这样使用它的: 在Js中:

const obj={
“longitudde”:lonLat[0],
“latudde”:lonLat[1]
}
获取(“/weather_request”,
{
方法:'POST',
标题:{
“内容类型”:“应用程序/json”
},
正文:JSON.stringify(obj)
})。然后(resp=>{
返回resp.json();
})。然后(res=>{
console.log(res)
//...
})
}
在控制器管线注释中,但可以与route.yaml一起使用:

使用Symfony\Component\Routing\Annotation\Route;
使用Symfony\Component\HttpFoundation\Request;
/**
*@Route(“/weather_request”,methods={“POST”})
*
*/
公共功能天气(请求$请求)
{
$data=json_decode($request->getContent());
$longitude=$data->longitudde;
$latitude=$data->latudde;
返回$this->render('weather/index.html.twig',
[
'weatherInfos'=>this->weatherService
->getWeather($经度,$纬度)
]);   

}
这样会有错误吗?@mohammedaysinechabli不,我只是不在url上传递任何信息