我在这里遇到问题。如何将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"],
];
任何人都可以帮助我吗?谢谢。 对不起,英语不好,也许很难理解
它可能不会被接受,因为您没有将Content-Type标头设置为'text/javascript'。在PHP文件的顶部做一个'header('Content-Type:text/javascript');'。 – Keelan 2015-04-03 08:53:01
@NarendraSisodia问题是我不能使它与locations.php工作 – 2015-04-03 08:55:02
@CamilStaps仍然无法正常工作,有没有解决方案?但感谢您的建议:D – 2015-04-03 08:56:17