我正在尝试学习 Jquery AJAX 函数,但正在努力弄清楚如何将 PHP 变量传递到我的主文档中。
这是我目前拥有的:
<script>
var refreshId = setInterval(function() {
$.ajax({
url: "test.php",
dataType: "json", //the return type data is jsonn
success: function(data){ // <--- (data) is in json format
$('#testdiv').html(data.test1);
$('#testdiv').append(html(data.test2));
//parse the json data
}
});
}, 1000);
</script>
最佳答案
你应该使用json
或者xml
格式解析,得到变量。
<script src="jquery.js"></script>
<script>
$.ajax({
url: "test.php",
dataType: "json", //the return type data is jsonn
success: function(data){ // <--- (data) is in json format
alert(data.test1);
//parse the json data
}
});
</script>
关于 test.php
<?php
$test = array();
$test['test1'] = '1';
$test['test2'] = '2';
$test['test3'] = '3';
echo json_encode($test);
//echo nothing after this //not even html
关于php - 通过 Jquery AJAX 传递 PHP 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5700122/