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); 

    3 answers 3

    1. 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

    1. Try to connect json in the html file in the script tag, and in js:

      var mydata = JSON.parse (data); alert (mydata [0] .name);

    • sorry but your answer is absolutely useless, see below for a solution - vexel

    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