简体   繁体   中英

How can a CodeIgniter controller accept an ajax post

I have a (CodeIgniter) VIEW that includes this...

<script language="javascript" type="text/javascript">
...
  $.ajax({
        type: "POST",
        url: "controller/preview_offer",
        data: jsonString, 
        cache: false,

        success: function(){
            alert("OK");
        }
    });        
...
</script>

Problem is that when I run it, I keep getting the "OK" alert, and absolutely nothing else.

My controller has a simple popup where I am hoping to find the jsonString coming through, but nothing happens at all.

My URL is formed just like I would use in a normal anchor() function, and I've tested it by putting in the explicit URL http://mydomain/index.php/controller/preview_offer - and it works.

I get the feeling that I've got an error here more fundamental than just syntax. What am I doing wrong?

You should change your success function to something like this:

success: function(data){
        alert("OK");
        $( "#results" ).append( data );
    }

And also for setting a controller URL in view use: site_url("controller/preview_offer");

your code will not run because of the url..try to change it a make it like this

$.ajax({
    type: "POST",
    url: <?php echo site_url("controller/preview_offer");?>,
    data: jsonString, 
    cache: false,

    success: function(){
        alert("OK");
    }
});        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM