如何使用$阿贾克斯()来传递一个字符串数组从PHP为Javascript? [英] How to pass an array of strings from PHP to Javascript using $.ajax()?
本文介绍了如何使用$阿贾克斯()来传递一个字符串数组从PHP为Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个从数据库中检索名称(串)的PHP脚本。我想用$就这个数组传递给Javascript的()。 我不明白我应该怎么连接code在PHP中的数组,然后去$ C C $它在Javascript中。 可能有人举个例子code这个? 非常感谢!
解决方案
< PHP // test.php的
$ = myArray的阵列(1,2,3);
回声json_en code($ myarray的);
?>
HTML文件:
$(函数(){
$ .getJSON('HTTP://localhost/test.php',功能(数据){
$(数据)。每个(函数(键,值){
//会提醒1,2和3
警报(值);
});
});
});
I have a PHP script that retrieves names (strings) from database. I would like to pass this array to Javascript using $.ajax(). I cannot understand how should I encode the array in PHP and then decode it in Javascript. Could someone give an example code for this ? Thanks a lot !!
解决方案
<?php // test.php
$myArray = array(1, 2, 3);
echo json_encode($myArray);
?>
HTML File:
$(function() {
$.getJSON('http://localhost/test.php', function(data) {
$(data).each(function(key, value) {
// Will alert 1, 2 and 3
alert(value);
});
});
});
这篇关于如何使用$阿贾克斯()来传递一个字符串数组从PHP为Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文