是的。如果你想让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