JSON PHP Array to Javascript

后端 未结 5 1689
刺人心
刺人心 2020-12-17 04:53

Is it possible to JSON this php array via json_encode? Because this php array is called $data and when i do..

var myJson = 

        
5条回答
  •  时光说笑
    2020-12-17 05:26

    As @Allendar said you can't embed PHP inside a JS file. You could, however, add a function in your JS file to load the JSON data, and then embed that data in a script tag in your PHP file.

    example.js:

    var loadJsonFromPHP = function(json) {
        console.log(json);
    }
    

    example.php:

    
    
        
            
            
        
        
    
    

    Edit: this is assuming you only need to get the data into JS once at page load, in which case you can skip making AJAX requests.

提交回复
热议问题