Welcome Guest, Not a member yet? Register   Sign In
Return values from CI controllers to Javacript (jQuery)
#11

[eluser]MungeGerard[/eluser]
Found the bug, the base url contained 'localhost' , somehow Windows7 or Easyphp doesn't work properly and only works with '127.0.0.1'.. Also I tested in two different environments.

Anyway thanks for all the help!!

I'll be back.

PS: I learned one thing: use json for ajax!
#12

[eluser]kkristo[/eluser]
You can use firebug for debug javascript. After javascript post/get call ypu can see, what variables are posted or not.
#13

[eluser]MaartenDeGroote[/eluser]
Hi All,

I also recently started using CodeIgniter. Up until now I really have been loving it.

However, I also seem to run into some small problems when using a POST $.ajax in jQuery. I was wondering if someone has run into the same problem before and knows an appropriate solution.

I created a simple form and use the serialize function from jQuery and the $.ajax method to submit the info to the controller. This looks like this:

Code:
$(document).ready(function(){
    $("form").submit(function(){
        var str = $(this).serialize();
        $.ajax({
            type : "POST",
            url : "ajax/form",
            data : str,
            success : function(msg){
                alert(msg.message)
            }
        }, "json");
        return false;
    });
});

Subsequently, the form method in the ajax class handles the request as following:

Code:
<?php
class Ajax extends Controller{
    
    function _construct(){
        parent::Controller();
    }
    
    function form(){
        $array = array("message" => "test");
        $result = json_encode($array);
        echo $result;
    }
    
}

No matter what I do, the return is always "undefined". I have been using this method earlier, before I was using CI so I figured there is something that I might have misconfigured in CI.

Any ideas? All help is appreciated!

Kind regards,

Maarten
#14

[eluser]rogierb[/eluser]
Hi Maarten,


When using CI , always make sure you use base_ur(); Especially when using ajax!
One of the most common problem is that the controller does not get called because of a problem in the path.
Code:
url : "<?php echo base_url();?>ajax/form",

As for you code, at glance, that is all I could find.
#15

[eluser]MaartenDeGroote[/eluser]
Well thanx very much Rogier (Nice to get the first response from another Dutch guy Wink)
However, I don't think this is the problem. When I replace the following code
Code:
alert(msg.message)
by
Code:
alert(msg)
The result is
Code:
{"message":"test"}

This means the controller is addressed properly right?

I keep on searching....
#16

[eluser]MaartenDeGroote[/eluser]
I figured this one out. Well, not exactly but I found a solution.
All i did was changing the following code:

Code:
$(document).ready(function(){
    $("form").submit(function(){
        var str = $(this).serialize();
        $.ajax({
            type : "POST",
            url : "ajax/form",
            data : str,
            success : function(msg){
                alert(msg.message)
            }
        }, "json");
        return false;
    });
});

to

Code:
$(document).ready(function(){
    $("form").submit(function(){
        var str = $(this).serialize();
        $.ajax({
            type : "POST",
            url : "ajax/form",
            data : str,
            dataType : "json",
            success : function(msg){
                alert(msg.message)
            }
        });
        return false;
    });
});

Now it works perfectly.
#17

[eluser]cahva[/eluser]
That is the expected behaviour. By default if you dont set the dataType, you just get the whole output tot the msg variable. Anyways, you should set the url with CI's url functions. You never know if you have to move the script elsewhere. I myself prefer using site_url function instead of base_url():
Code:
url : "<?php echo site_url('ajax/form') ?>",




Theme © iAndrew 2016 - Forum software by © MyBB