我在這裏遇到問題。如何將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