Welcome Guest, Not a member yet? Register   Sign In
Post method not working in Codeigniter
#1

[eluser]vikas.fourorange[/eluser]
This is my first question on CI forms.Which obviously shows that I am a noob in codeigniter.Please help.

I am using HMVC codeigniter. I am trying to use jquery ajax first time. When i use POST then it gives undefined error while it response me the data while using GET.

Code:
$.ajax({
     type: "POST",
     url: filelink+"cart/add_cart_item",
     data: {"product_id":id,"quantity":qty,"ajax":"1"},
     dataType: "json",
            success: function(msg){
      alert( "Data Saved: " + msg );
        },
             error: function(jqXHR, textStatus, errorThrown){
      alert(textStatus + " " + errorThrown);
       }
      });
What I have tried so far after googling

a) my file url location is accessible directly. I checked it. giving response.

b) Firebug is giving 500 internal server error for the same file.

c) Using Get is responding me back well

d) added json in the datatype

controller

Code:
class Cart extends CI_Controller { // Our Cart class extends the Controller class
      
      function __construct()
      {
  parent::__construct();
  $this->template->set('controller', $this);
      }

   function _remap()
     {
     $uri2 = $this->uri->segment(2);
     if (is_numeric($uri2) OR $uri2 == FALSE) {
   $this->index();
       } else if ($uri2 == 'add_cart_item') {
   $this->add_cart_item();
     } else if ($uri2 == 'show_cart') {
   $this->show_cart();
     }
      }

function add_cart_item(){
       echo "asdfsadfdsf";
       exit;
     }
      }

can anybody please help me out?

#2

[eluser]Jason Hamilton-Mascioli[/eluser]
Maybe the url parameter needs some modification...

Code:
url: "/cart/add_cart_item",
#3

[eluser]vikas.fourorange[/eluser]
Do you want me to remove the base url from the code ??
#4

[eluser]Jason Hamilton-Mascioli[/eluser]
Yes, try a relative link and or hardcode and test to see if something works. I believe the link is not setting correctly.
#5

[eluser]vikas.fourorange[/eluser]
Yes I tried the same but failed. Still giving the error 500 internal server error at the firebug..
#6

[eluser]InsiteFX[/eluser]
Try adding this, it will setup the two for use in jQuery url links.
Code:
// Replace the $ sign in both script tags with s
<head>
<$cript type="text/javascript" charset="utf-8">
  //&lt;![CDATA[
   var base_url = "&lt;?php echo base_url(); ?&gt;";
   var site_url = "&lt;?php echo site_url(); ?&gt;";
  // ]]>
</$cript>
&lt;/head&gt;

// jQuery code:
$.ajax({
     type: "POST",
     url: base_url+"cart/add_cart_item",
     data: {"product_id":id,"quantity":qty,"ajax":"1"},
     dataType: "json",
            success: function(msg){
      alert( "Data Saved: " + msg );
        },
             error: function(jqXHR, textStatus, errorThrown){
      alert(textStatus + " " + errorThrown);
       }
      });
#7

[eluser]vikas.fourorange[/eluser]
I am sorry but I am not sure about your reply. Do you want me to add &lt;?php base_url() ?&gt; in the script ?? I am already having the base url for getting the path.

Thanks
#8

[eluser]InsiteFX[/eluser]
Add the head stuff to your html head then you can use base_url and site_url in your jQuery scripts.

As you can see in the Ajax url:

The site_url wil have the index.php with it.
#9

[eluser]vikas.fourorange[/eluser]
I tried the same you mentioned but I am not sure about your line "As you can see site url will have index.php" ??

I put the document.ready function in the head tag.and base url and site url both in the script .

Both site_url and base_url are same.
#10

[eluser]vikas.fourorange[/eluser]
Managed to find the solution. The problem was due to CI_TOKEN which is sent with the FORM. That was absent and due to which POST method was giving 500 Internal server Error. I added following in my view file.

Code:
&lt;?php echo form_open_multipart(); ?&gt;
&lt;?php echo form_close(); ?&gt;
and sent ci_token with the ajax post request.
Code:
var ci_token = formObj.find('input[name=ci_token]').val();
var qty = 1;
var dataString = 'product_id='+ id + '&quantity;=' + qty + '&ajax;=' + 1
+'&ci;_token=' + ci_token;

This solved the problem. I am not sure but this is called CSRF related some problem

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB