2015-04-03 105 views
0

我在这里遇到问题。如何将json数据传递给.js文件

我有这样的代码在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> 

我想将JSON传递给locations.js创建JSON数据。它将通过另一个带有getScript函数的.js文件调用。我试着拨打locations.php但其没有工作,所以我创建locations.js测试和它的工作原理

不工作

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

工作

$.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"], 
]; 

任何人都可以帮助我吗?谢谢。 对不起,英语不好,也许很难理解

+1

它可能不会被接受,因为您没有将Content-Type标头设置为'text/javascript'。在PHP文件的顶部做一个'header('Content-Type:text/javascript');'。 – Keelan 2015-04-03 08:53:01

+0

@NarendraSisodia问题是我不能使它与locations.php工作 – 2015-04-03 08:55:02

+0

@CamilStaps仍然无法正常工作,有没有解决方案?但感谢您的建议:D – 2015-04-03 08:56:17

回答

2

林不知道你需要什么。 你的PHP文件需要输出中的JSON对象: 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)); 

    ?> 

然后你可以使用jQuery

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

如果要加载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") 
     ); 
?> 
var locations = <?= json_encode($locations) ?>; 

然后你可以通过jQuery.getScript加载它。

+0

这并不能解释它为什么会使用location.js。 – Keelan 2015-04-03 08:56:57

+0

它与location.js一起工作,因为location.js是一个有效的JavaScript文件。尝试运行你的location.php文件,将输出保存为.js文件并在主页面中引用它,它会触发一个错误,意味着你有语法错误。一个js文件不能包含标记。 – RafH 2015-04-03 09:04:54

+0

谢谢这么多,我试过最后一个,一切都很好。非常感谢^^ – 2015-04-03 09:07:44

1

首先,从PHP文件中删除script标记。它的输出应该与你的locations.js类似。

如果在此之后仍然不起作用,则可能需要设置Content-Type标题。添加,在你的PHP文件的顶部:

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

此行应该去之前任何发送到输出流。