CodeIgniter Forums
Jquery $.post JSON returns html - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Jquery $.post JSON returns html (/showthread.php?tid=40488)



Jquery $.post JSON returns html - El Forum - 04-10-2011

[eluser]froginvasion[/eluser]
I'm trying to get my controller to return a JSON object, but it doesn't
work like it works in all tutorials I can find online (probably pre 2.0 I guess)

some code:
Code:
var u_id = $('input[name=usergroup_id]').val();
                $.post('ajax/group/delete_link',{'q_id': $(this).text(),'u_id': u_id},function(data){
                   alert(data);
                });

Code:
function delete_link() {
        $id = $this->input->post('u_id');
        $result = array('test' => var_dump($id));

        echo json_encode($result);
    }

It just returns HTML that is the same from the page that is already loaded,
and I don't know why, is there anybody that can help?
Should there be headers set or something, to define that I'm returning
a JSON object?


Jquery $.post JSON returns html - El Forum - 04-10-2011

[eluser]dianikol85[/eluser]
in ci 2 you need to pass the token along with the other parameters


Jquery $.post JSON returns html - El Forum - 04-10-2011

[eluser]froginvasion[/eluser]
I found the problem, but for some reason I can't edit my post or do anything with it.

The URL that is used was wrong, so it just returned index(), which is why it returned
HTML.


Jquery $.post JSON returns html - El Forum - 04-10-2011

[eluser]dianikol85[/eluser]
[quote author="froginvasion" date="1302457669"]I'm trying to get my controller to return a JSON object, but it doesn't
work like it works in all tutorials I can find online (probably pre 2.0 I guess)

some code:
Code:
var u_id = $('input[name=usergroup_id]').val();
                $.post('ajax/group/delete_link',{'q_id': $(this).text(),'u_id': u_id},function(data){
                   alert(data);
                });

Code:
function delete_link() {
        $id = $this->input->post('u_id');
        $result = array('test' => var_dump($id));

        echo json_encode($result);
    }

It just returns HTML that is the same from the page that is already loaded,
and I don't know why, is there anybody that can help?
Should there be headers set or something, to define that I'm returning
a JSON object?[/quote]


By the way i think you must tell to the $.post that it will accept json


Code:
var u_id = $('input[name=usergroup_id]').val();
                $.post('ajax/group/delete_link',{'q_id': $(this).text(),'u_id': u_id},function(data){
                   alert(data);
                },'json');



Jquery $.post JSON returns html - El Forum - 04-10-2011

[eluser]InsiteFX[/eluser]
CodeIgniter - CSRF Protection by Eric Barnes

InsiteFX