0

I have jquery like this but it seems it cannot read the controller/function

$('#submit').click(function(){

    $.ajax(
    {
    url:'controller/function',
    type:"POST",
    success:function(data){

         $(".content").load(url); 

        }
    }
    )
});

can anyone tell me whats wrong or give other solution.

3
  • it cannot read the controller/function since i bet you gave a worng path.. that is why it cannot read the controller function...try using proper path.. Commented Feb 4, 2013 at 8:20
  • Have you tried giving url like this - url:"@Url.Content("/controller/function")",Also try specifying dataType property. Commented Feb 4, 2013 at 8:23
  • thanks for the codes but when i click the button nothing happens Commented Feb 4, 2013 at 9:20

3 Answers 3

1

This is a solution I came up with awhile ago.

In your html head tag add this.

 <script>
      var site = "<?php echo site_url(); ?>";
 </script>

Then in your .js file you can do:

$('#submit').click(function(){

    $.ajax(
    {
    url:base + 'controller/function',
    type:"POST",
    success:function(data){

         $(".content").load(url); 

        }
    }
    )
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks , i tried this but when i click the button nothing happens
Nothing happens? Does the click function perform. If so, is there an error being returned? Add this to your ajax call. success:function(data){ }, error: function(response){ console.log(response); // also try response.responseText }
0

I do believe that you use wrong path, do you have firefox, you can use firebug to get the response, what the response code is? and if you use spring mvc and log4j, you can set the log level to debug, then you can see what the url that you send to the server as well as whether it is got matched?

2 Comments

can you give step by step how to do this?
1. install firefox and open you website with it. 2. install firebug plug-in. 3. move to the firebug console tab. 4. click the button you mentioned and pay attention to that tab it will show the url that you can as well as the post parameter and the response information. this can give you the hint what happened.
0

use site_url() function of codeigniter...

<?php echo site_url('controllername/functionname') ?>

The above example would return something like: http://example.com/index.php/controllername/functionname;

try this

$.ajax(
{
   url:<?php echo site_url('controllername/functionname') ?>,
   type:"POST",
   success:function(data){

     $(".content").load(url); 

    }
}

updated as per @kolby comment

if you have this script in seperate js file thn..

create a global var base_url in head so that u get this variable in every file.

<head>
....//your stuff
<script>var base_url= <?php echo site_url();  ?></script>
</head>

and use this in ajax

url:base_url+'controllername/functionname',

1 Comment

its still the same when i click the button nothing happens it doesn't go anywhere

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.