如何将json数据传递给.js文件

i got a problem here .

I have this code to create the json data in locations.php

<?php
    $locations = array(
            array('2479 Murphy Court', "Minneapolis, MN 55402", "$36,000", 48.87, 2.29, "property-detail.html", "assets/img/properties/property-01.jpg", "assets/img/property-types/apartment.png"),
            array('3398 Lodgeville Road', "Golden Valley, MN 55427", "$28,000", 48.866876, 2.309639, "property-detail.html", "assets/img/properties/property-02.jpg", "assets/img/property-types/apartment.png")
        );
?>

<script type="text/javascript">
    var locations = "<?= json_encode($locations) ?>";
</script>

I want to pass the json to locations.js . Which it will called by another .js file with getScript function. I tried to call the locations.php but its not working, so i create locations.js to test and it works

Not working

$.getScript("assets/php/locations.php", function(){

Working

$.getScript("assets/js/locations.js", function(){

locations.js

var locations = [
    ['2479 Murphy Court', "Minneapolis, MN 55402", "$36,000", 48.87, 2.29, "property-detail.html", "assets/img/properties/property-01.jpg", "assets/img/property-types/apartment.png"],
    ['3398 Lodgeville Road', "Golden Valley, MN 55427", "$28,000", 48.866876, 2.309639, "property-detail.html", "assets/img/properties/property-02.jpg", "assets/img/property-types/apartment.png"],
];

Can anyone help me? Thank you. Sorry for bad english and maybe hard to understand

Im not sure to understand well what you need. Your PHP file need to ouput the JSON object : location.php

    <?php

$locations = array(
    array('2479 Murphy Court', "Minneapolis, MN 55402", "$36,000", 48.87, 2.29, "property-detail.html", "assets/img/properties/property-01.jpg", "assets/img/property-types/apartment.png"),
    array('3398 Lodgeville Road', "Golden Valley, MN 55427", "$28,000", 48.866876, 2.309639, "property-detail.html", "assets/img/properties/property-02.jpg", "assets/img/property-types/apartment.png")
);
header('Content-Type: application/json');
die(json_encode($locations));

    ?>

Then you can load it with jquery

$.getJSON("location.php", function(data) {
  // do whatever you want with data
  console.log(data);
});

If you want to load the script that location.php output

<?php
    $locations = array(
            array('2479 Murphy Court', "Minneapolis, MN 55402", "$36,000", 48.87, 2.29, "property-detail.html", "assets/img/properties/property-01.jpg", "assets/img/property-types/apartment.png"),
            array('3398 Lodgeville Road', "Golden Valley, MN 55427", "$28,000", 48.866876, 2.309639, "property-detail.html", "assets/img/properties/property-02.jpg", "assets/img/property-types/apartment.png")
        );
?>
var locations = <?= json_encode($locations) ?>;

Then you can load it via jQuery.getScript.

First of all, remove the script tag from the PHP file. Its output should look similar to your locations.js.

If it still doesn't work after that, it may be you need to set the Content-Type header. Add, in the top of your PHP file:

header('Content-Type: text/javascript');

This line should go before anything is sent to the output stream.