• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling to a method controller from jQuery

#1
[eluser]Jose Dueñas[/eluser]
Hi,
I've a controller which inserts into a databse an user:

Code:
class Users extends Controller {
    
    function insert_user()
    {
             $this->Usersmodel->insert("John Smith");
    }
}

What I want to do is do that with jQuery, I've tried from the view:

Code:
$.ajax({url:"http://localhost/index.php/users/insert_user",
                success: function() {
                    // Request has been successfully submitted
                    alert("Succeeded");            
                },
                  error: function() {
                    // An error occurred, do something about it
                    alert("Failed");
                   },
                complete: function() {
                    // We're all done so do any cleaning up.
                    alert("Process has been done");
                }
               });

And I always getting "Failed". It's because I'm trying to load something is not a script.

How could I do this?

Thanks,
Jose

#2
[eluser]TheFuzzy0ne[/eluser]
I think your controller method needs to echo something back to your script.

#3
[eluser]Jose Dueñas[/eluser]
[quote author="TheFuzzy0ne" date="1237824800"]I think your controller method needs to echo something back to your script.[/quote]

I've added:
Code:
echo "HEllo World";

to my controller but still fails.

Regards,
jose

#4
[eluser]Jose Dueñas[/eluser]
Ok, I've resolved this. (thanks to my friend Firebug debugger, hehe).

This code works:

Code:
$.ajax({type: "POST", url:"http://localhost/index.php/users/insert_user/",
                success: function() {
                    // Request has been successfully submitted
                    alert("Succeeded");            
                },
                  error: function() {
                    // An error occurred, do something about it
                    alert("Failed");
                   },
                complete: function() {
                    // We're all done so do any cleaning up.
                    alert("Process has been done");
                }
               });

So, I've added the "type" and the "/" after the http.

Thanks,
Jose

#5
[eluser]TheFuzzy0ne[/eluser]
Well done! Smile

#6
[eluser]jdfwarrior[/eluser]
Yeah I was gonna say to try that or recommend that instead of using the $.ajax command, suggest that you use simply the $.post command. I dont think the / at the end matters.

#7
[eluser]TheFuzzy0ne[/eluser]
It does when you are appending data to it. Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.