doujingjiao0015 2015-04-03 08:50 采纳率: 0%
浏览 317
已采纳

如何将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

  • 写回答

2条回答 默认 最新

  • dor65412 2015-04-03 08:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!