Welcome Guest, Not a member yet? Register   Sign In
Unable to get jquery ajax working
#1

[eluser]ssachan[/eluser]
Hello Developers,
I am struggling to get jquery's $post method to get working, here is my code snippet

my jquery code in my view looks like :

$(document).ready(function(){
$(".button").click(function(){
$.post('/welcome/get_messages/', {ajax:'true'},
function(data){
alert(data);
}, 'json');
});
});

the controller welcome.php has the get_messages function that has a simple echo

function get_messages()
{
echo "Hello";

}


however my alert(data) gives me a null.
I am guessing that the URL in post command is incorrect, but then I have tried all the
following as well
'<?=base_url()?>application/controllers/welcome/get_messages'
'<?=base_url()?>/index.php/application/controllers/welcome/get_messages'
but none seem to work.

Please suggest me something that can get my code working.

Thanking in anticipation

Shikhar
#2

[eluser]Federico BaƱa[/eluser]
Use the URL like this: "/index.php/welcome/get_messages", or if you changed the URL using .htaccess then remove the /index.php and send "/welcome/get_messages"

Let me know if that works
#3

[eluser]napz[/eluser]
yeah i think you haven't modify yet the .htaccess.
#4

[eluser]pickupman[/eluser]
You have specified the data type of json in your post function. You need to echo json back out, or change the data type to 'html'.
try:
Code:
function get_messages(){
  $data['message'] = 'hello';
  echo json_encode($data);
}

//In jquery
function(data){
  alert(data.message);
}
#5

[eluser]ssachan[/eluser]
Hello pickupman and others,

Thanks for your comments
I made the following changes
Code:
function get_messages(){
  $data['message'] = 'hello';
  echo json_encode($data);
}

//In jquery
$(".button").click(function(){
    debugger;
    alert('yo');        
    $.post('<?=site_url('welcome/get_messages');?>', {ajax:'true'},function(data){
                          alert(data.message);
                    }, 'json');
                });

As shown I have also made changes in the url using the site_url function. Still no success.
Is there something that I am missing. Do I need to configure something to get ajax working?
#6

[eluser]pickupman[/eluser]
Here is a working copy. You can download the zip and check it out for yourself. I am not sure what the debugger; line is, but anything that is not valid js will screw anything after it.
Also, if you are not using firefox's firebug, get it. Enable the console tab, and watch all the ajax glory.
#7

[eluser]ssachan[/eluser]
Hi thanks a lot for the code.
But as I said, its the same thing that I tried and it isn't working. The console gives me
myurl/welcome/get_messages and then says 'data' is null.
The url pointed is correct since opening in the browser echo's hello Ajax.
Any other hints.
thanks
#8

[eluser]danmontgomery[/eluser]
http://getfirebug.com/ this.
#9

[eluser]pickupman[/eluser]
You got me. It is working exactly as you would guess. Here's a screen capture. Just for giggles, have you tried another browser? Did you use the files exactly as they were in the zip file?
#10

[eluser]ssachan[/eluser]
Hi pickupman
Thanks for your help, but returning a false at the end of onClick solved my problem. Don't know the reason why? but if you have a clue, it would give me a better idea.
I am on firefox with firebug on Mac




Theme © iAndrew 2016 - Forum software by © MyBB