Tell me how in Yii2 to get in js data from a JSON file that is in the same folder with a js file, did like this but gives an error (404):
var jsonResponse = $.getJSON( "test.json"); console.log(jsonResponse); Tell me how in Yii2 to get in js data from a JSON file that is in the same folder with a js file, did like this but gives an error (404):
var jsonResponse = $.getJSON( "test.json"); console.log(jsonResponse); It is worth trying this design:
$ .getJSON ("json.js", function (json) {alert ("JSON Data:" + json);});
and read: https://javascript.ru/forum/jquery/17308-poluchenie-json.html
Try to connect json in the html file in the script tag, and in js:
var mydata = JSON.parse (data); alert (mydata [0] .name);
the question was resolved, I had to do so
var conf = function() { return ('../js/conf.json'); } or create a valid url using urlHelper
<?= 'var url_base = "' . \yii\helpers\Url::base() .'";'; ?> var conf = function() { return ( url_base + '/js/conf.json'); } Or maybe just?
var conf = function() { return ('./conf.json'); } In a sense, accessing a file in the same directory is usually
./test.json Source: https://ru.stackoverflow.com/questions/844487/
All Articles