I can not get the script to work in Yii2 to get json data from the database. there is a file / modules/maps/views/object/objectsjson.php (simplified .., in general, data is taken from the database):

echo '{ "type": "FeatureCollection", "features": [ {"type": "Feature", "id": 0, "geometry": {"type": "Point", "coordinates": [55.831903, 37.411961]}, "properties": {"balloonContent": "Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠΈΠΌΠΎΠ΅ Π±Π°Π»ΡƒΠ½Π°", "clusterCaption": "Π•Ρ‰Π΅ ΠΎΠ΄Π½Π° ΠΌΠ΅Ρ‚ΠΊΠ°", "hintContent": "ВСкст подсказки"}}, {"type": "Feature", "id": 1, "geometry": {"type": "Point", "coordinates": [55.763338, 37.565466]}, "properties": {"balloonContent": "Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠΈΠΌΠΎΠ΅ Π±Π°Π»ΡƒΠ½Π°", "clusterCaption": "Π•Ρ‰Π΅ ΠΎΠ΄Π½Π° ΠΌΠ΅Ρ‚ΠΊΠ°", "hintContent": "ВСкст подсказки"}} ] }'; 

and there is a javascript that accesses this file (an example is taken from here ):

 ymaps.ready(init); function init () { var myMap = new ymaps.Map('map', { center: [сenter1,сenter2], zoom: 1, type: 'yandex#satellite' }), objectManager = new ymaps.ObjectManager({ clusterize: true, gridSize: 32 }); myMap.geoObjects.add(objectManager); $.ajax({ url: "http://site.ru/basic/web/index.php?r=maps%2Fobject%2Fobjectsjson" }).done(function(data) { objectManager.add(data); }); } 

the output is empty .. but if you put this file in the web folder and register

 $.ajax({ url: "objectsjson.php" }).done(function(data) { objectManager.add(data); }); 

then it works! In the controller, that's what?

 public function actionObjectsjson() { return $this->render('objectsjson'); } 

Where is the mistake? Thank you in advance.

  • In the controller, this is what: - Monitorkin

1 answer 1

In $json your json file

  public function actionObjectsjson() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return $json; }