2015-04-03 114 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'); 

此行應該去之前任何發送到輸出流。