是的。如果你想讓PHP和Javascript一起工作,答案是:JSON。你將不得不在JSON中編碼你的數組。使用Javascript Ajax調用PHP文件,然後parseJSON響應。然後你在Javascript中使用你的數組。之後,你可以在Javascript中渲染它,並將結果附加到你想要的元素。
jQuery文檔中有很多例子。如果你對ajax不熟悉,請閱讀文檔:http://api.jquery.com/jQuery.ajax/最後有一些簡單的例子。您也可以使用.getJSON。
jQuery.getJSON(url, [data,] [success(data, textStatus, jqXHR)])
- url A string containing the URL to which the request is sent.
- data A map or string that is sent to the server with the request.
- success(data, textStatus, jqXHR) A callback function that is executed if the request succeeds.
基本上是:
$.getJSON("url of the php file",
data to send to the php file if any,
function(data) {
console.log(data);
}
});
內部的功能在哪裏,你可以做你想做你的數據什麼的的console.log。
好的,我已經在我的PHP腳本中使用了json_encode,我該如何使用Ajax調用來在JavaScript/JQuery中檢索它? – trottski