PHP:无法获取json输出到我的javascript页面作为一个变量值 [英] PHP: Unable to get the json output into my javascript page as a variable value
问题描述
我必须以这种格式输出
var sampleTags = ['c ++','scala'];
i have to get output in this format
var sampleTags = ['c++', 'scala'];
我的javascript功能是:
My javascript function is:
<script>
$(document).ready(function(){
$(function(){
var sampleTags;
$.ajax({
url:"<?php echo base_url('ajax_get_tags/gettags'); ?>"
}).done(function(data) {
if (data) {
sampleTags = data;
}
});
......................
.......................
$(function(){
var sampleTags = <?php echo json_encode($query) ?>;
我的php控制器是
function gettags(){
$json_array=$this->tagsmodel->get_all_tags();
echo json_encode($json_array);
}
我的模型是
//-------------------------------Function get all tags--------------------------------
function get_all_tags() {
$this->load->database();
$this->db->limit('10');
$this->db->select('tags_name');
$res = $this->db->get('tags');
$ret = array();
foreach ($res->result_array() as $row) {
$ret[] = $row['tags_name'];
}
return $ret;
}
如何获取ajax请求的json输出javascript变量?
请帮我解决这个问题..
How can get the json output from ajax request to be display its value for a javascript variable? Please help me to solve this issue..
推荐答案
您使用的是旧版本的jQuery, code> .done 将无法工作。看起来您希望向名为 complete
的请求对象添加键,并使用匿名函数作为其值:
You're using an older version of jQuery, so .done
won't work. It looks like you want to add a key to your request object called complete
, with the anonymous function as its value:
$.ajax({
url: "<?php echo base_url('ajax_get_tags/gettags'); ?>",
complete: function(data) {
if (data) {
sampleTags = data;
}
}
});
我通过查看您的错误消息找到了这一点。其中一个结果是这个问题: Object#< XMLHttpRequest>没有方法完成。你可以只是google了错误信息,并自己想出了这一点。
I found this out by googling your error message. One of the results was this question: Object #<XMLHttpRequest> has no method 'done'. You could have just googled the error message and figured this out yourself.
这篇关于PHP:无法获取json输出到我的javascript页面作为一个变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!