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]napz[/eluser]
I really dont know how to get a jquery ajax, however I tried ajax in prototype using code igniter it works like pretty the same.

you should post in the target controller class you want. Pretty much like posting using form
#3

[eluser]Thorpe Obazee[/eluser]
Do you have firebug installed? if you have firebug installed, it would be easy to see if you're pointing to a 404 page?

Anyway, you should have

Code:
$(document).ready(function(){
  $(”.button”).click(function(){
      $.post('<?php echo site_url('welcome/get_messages');?>’, {ajax:'true'},
      function(data){
          alert(data);
      }, ‘json’);
    });
});

You might also be interested in this post:

jquery and ajax
#4

[eluser]napz[/eluser]
Hey PDP,

nice website pinoytech.org... kababayan hehehhehe...
#5

[eluser]Thorpe Obazee[/eluser]
[quote author="napz" date="1271996409"]Hey PDP,

nice website pinoytech.org... kababayan hehehhehe...[/quote]

thanks. Smile
#6

[eluser]ssachan[/eluser]
Hello developers,

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

[eluser]Kamarg[/eluser]
Edit: Ignore this. I misread the previous post.
#8

[eluser]BradEstey[/eluser]
You need to add a return false to the end of the click function.
Code:
$(document).ready(function(){
                $(".button").click(function(){
                    $.post("welcome/get_messages/", {ajax:'true'},
                        function(data){
                            alert(data.message);
                        }, "json");    
                  return false;                    
                });
            });

Hope that helps.
#9

[eluser]ssachan[/eluser]
thanks a lot !!! problem solved. But can you explain me the reason why not returning a false causes NULL values to appear ?
#10

[eluser]BradEstey[/eluser]
[quote author="ssachan" date="1272068156"]thanks a lot !!! problem solved. But can you explain me the reason why not returning a false causes NULL values to appear ?[/quote]

Well it depends on what the element is that's being clicked, but basically it just tells jQuery interupt whatever natural function an element would have performed. So if .button is an anchor, it will keep it from moving forward to a new page.




Theme © iAndrew 2016 - Forum software by © MyBB