Welcome Guest, Not a member yet? Register   Sign In
Jquery $.post JSON returns html
#1

[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?
#2

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

[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.
#4

[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');
#5

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

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB