使用codeigniter在ajax中获取响应数据

时间:2022-11-24 14:36:19

Hi i have this data which i want that only a certain div will refresh using ajax. How will i able to pass the json_encode to the ajax what will load the data on a certain div using codeigniter. Heres my controller below

嗨我有这个数据,我希望只有一个div将使用ajax刷新。我如何能够将json_encode传递给ajax,它将使用codeigniter加载某个div上的数据。下面是我的控制器

$this->data['getNumberOfMessages'] = $this->mm->getNumberOfMessages($this->data['id']);
$this->data['countNumber'] = $this->data['getNumberOfMessages'];

$responseOnject = new stdClass();
$responseOnject->status = 'ok';
$responseOnject->StatusMessages=$this->data['countNumber'];

$this->output->set_output(json_encode($responseOnject));

this line of code here

这行代码在这里

$this->output->set_output(json_encode($response));

outputs this one

输出这一个

{"status":"ok","StatusMessages":20}

and i want that to pass through my ajax in my code below

我想在下面的代码中传递我的ajax

$(document).ready(function(){
     var id =$(this).data("messageid");
     $.ajax({
        type: "get",
        url: $(this).data("href"),

        success: function(data){
            alert(data.StatusMessages);
            console.log(data.StatusMessages);

            $("#" + id + ' span.badge').html(data.StatusMessages);
        }
    });
});

and target on my certain div so that it will automatically refresh below is my code

并定位在我的某个div上,以便它自动刷新下面是我的代码

<tr>
    <th>
        <a href="#" id="<?php echo $id; ?>" data-messageid="<?php echo $id; ?>" data-href="<?php echo base_url().'profile'?>" title="messages">
        <span class="badge"><?php echo $countNumber; ?></span>
                    message(s)</a>
    </th>
</tr>

when i tried to alert(data.StatusMessages);it return undefined. can someone pls help me figured this thing out? ive been stuck in this.. my code really works but i want to be ajax, only certain div refresh without refershing my entire page. Any help is muchly appreciated.

当我试图提醒(data.StatusMessages);它返回undefined。有人可以帮我解决这个问题吗?香港专业教育学院一直困在这..我的代码真的有效,但我想成为ajax,只有某些div刷新而没有引用我的整个页面。任何帮助都非常感谢。

2 个解决方案

#1


0  

You need to decode the response JSON first in order to be able to access it as a JS object. This is how it's done in JS:

您需要首先解码响应JSON,以便能够将其作为JS对象进行访问。这是在JS中完成的:

JSON.parse(JSONStringHere);

So, your success callback should be:

所以,你的成功回调应该是:

success: function(data){
      data = JSON.parse(data); // this is what decodes JSON
      alert(data.StatusMessages);
      console.log(data.StatusMessages);

      $("#" + id + ' span.badge').html(data.StatusMessages);
    }

As I said in the comments, you're not calling the right URL with AJAX (although you said you do and that it returns that JSON string; if so, the code above would work). This doesn't look like it would give you a URL:

正如我在评论中所说的那样,你没有用AJAX调用正确的URL(尽管你说过它并且它返回了JSON字符串;如果是这样,上面的代码就可以了)。这似乎不会给你一个URL:

$(document).ready(function(){
  var id =$(this).data("messageid");
  $.ajax({
    type: "get",
    url: $(this).data("href"), // <---- what is the value of this?

Once you fix that, your code will work.

一旦你解决了这个问题,你的代码就可以了。

#2


0  

For JSON data your controller should have the following line to output

对于JSON数据,您的控制器应具有以下输出行

UPDATE

Just noticed your problem is that codeigniter is outputting html along side your ajax response.

刚刚注意到你的问题是codeigniter正在输出你的ajax响应的html。

To Make this go away, Your code in the controller should be like this:

要使其消失,控制器中的代码应如下所示:

$this->output
     ->set_content_type('application/json', 'utf-8')
     ->set_output(json_encode($responseOnject))
     ->_display();
exit;

This would force the output library to display & the exit to stop displaying anything else.

这将强制输出库显示和退出以停止显示任何其他内容。

If you do not set the application/json content type, your response won't be treated as JSON And you'd have to use JSON.parse in your javascript code to convert it to json which is a bad practice.

如果你没有设置应用程序/ json内容类型,你的响应将不会被视为JSON你必须在你的javascript代码中使用JSON.parse将它转换为json这是一个不好的做法。

#1


0  

You need to decode the response JSON first in order to be able to access it as a JS object. This is how it's done in JS:

您需要首先解码响应JSON,以便能够将其作为JS对象进行访问。这是在JS中完成的:

JSON.parse(JSONStringHere);

So, your success callback should be:

所以,你的成功回调应该是:

success: function(data){
      data = JSON.parse(data); // this is what decodes JSON
      alert(data.StatusMessages);
      console.log(data.StatusMessages);

      $("#" + id + ' span.badge').html(data.StatusMessages);
    }

As I said in the comments, you're not calling the right URL with AJAX (although you said you do and that it returns that JSON string; if so, the code above would work). This doesn't look like it would give you a URL:

正如我在评论中所说的那样,你没有用AJAX调用正确的URL(尽管你说过它并且它返回了JSON字符串;如果是这样,上面的代码就可以了)。这似乎不会给你一个URL:

$(document).ready(function(){
  var id =$(this).data("messageid");
  $.ajax({
    type: "get",
    url: $(this).data("href"), // <---- what is the value of this?

Once you fix that, your code will work.

一旦你解决了这个问题,你的代码就可以了。

#2


0  

For JSON data your controller should have the following line to output

对于JSON数据,您的控制器应具有以下输出行

UPDATE

Just noticed your problem is that codeigniter is outputting html along side your ajax response.

刚刚注意到你的问题是codeigniter正在输出你的ajax响应的html。

To Make this go away, Your code in the controller should be like this:

要使其消失,控制器中的代码应如下所示:

$this->output
     ->set_content_type('application/json', 'utf-8')
     ->set_output(json_encode($responseOnject))
     ->_display();
exit;

This would force the output library to display & the exit to stop displaying anything else.

这将强制输出库显示和退出以停止显示任何其他内容。

If you do not set the application/json content type, your response won't be treated as JSON And you'd have to use JSON.parse in your javascript code to convert it to json which is a bad practice.

如果你没有设置应用程序/ json内容类型,你的响应将不会被视为JSON你必须在你的javascript代码中使用JSON.parse将它转换为json这是一个不好的做法。