【问题标题】:Post method not working in CodeigniterPost 方法在 Codeigniter 中不起作用
【发布时间】:2012-06-28 12:18:25
【问题描述】:

我正在使用 HMVC 代码点火器。我第一次尝试使用 jquery ajax。当我使用 POST 时,它会在使用 GET 时响应我的数据时给出未定义的错误。

         $.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);
            }
      });

在谷歌搜索和 SO-ing 之后我到目前为止所尝试的 -

  1. 我的文件 url 位置可以直接访问。我检查了它。给予回应。

  2. Firebug 为同一文件提供 500 个内部服务器错误。

  3. 使用 Get 对我的反应很好

  4. 在数据类型中添加了 json

控制器功能

  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; 
    }
  }

谁能帮帮我?

【问题讨论】:

  • 你试过var_dump($this->input->post('product_id')); 吗?它有输出吗?
  • @vikassharma:filelink 包含什么?
  • @vikassharma 你的 base_url() 是什么样的?您是否尝试过使用完全限定的 url?或者只是控制器的名称加上方法?
  • @vikassharma 如果您正常访问,您的控制器是否正常工作?我看到您实际上使用的是 hmvc,但您仍在扩展 ci_Controller 而不是 Mx_controller。
  • @Kyokasuigetsu 我对这个 codeigniter 和 hmvc 很陌生。实际上这是其他控制器的设计方式。其他页面使用 main.php 作为外部模板文件,并且内容通过加载部分显示在它们之间。可能是这引起了问题????他们都在使用 ci_controller。我复制了相同的 ....

标签: php jquery ajax codeigniter


【解决方案1】:

您的问题有可能的原因

您可能正在使用未加载的模型。
模型中可能存在一些代码问题。
您可能不会从模型中返回任何内容并对其进行回显。
此外,如果您需要返回数据,请使用 echo json_encode($data)

【讨论】:

  • 嗨,Raheel,我编辑了我的代码。请看一下控制器。我没有使用任何模型。这可能是问题吗??
【解决方案2】:

当您将网站加载为 https://example.com 但您的 ajax 调用中的 url 是 http://example.com 时,有时会出现此问题。

【讨论】:

  • 但是我用的是同一个 base_url() ?> 通过整个网站。网址就好了..
【解决方案3】:

设法找到解决方案。问题是由于 CI_TOKEN 与 FORM 一起发送的。那是不存在的,并且由于哪个 POST 方法给出了 500 内部服务器错误。我在视图文件中添加了以下内容。

 <?php echo form_open_multipart(); ?>
 <?php echo form_close(); ?>

并使用 ajax 发布请求发送 ci_token。

 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;

这解决了问题。我不确定,但这被称为 CSRF 相关的一些问题

谢谢

【讨论】:

    【解决方案4】:

    您使用了 _remap 函数,在获得 $uri2 后,有一个 if 检查,它正在检查您在 ajax 请求中传递的数量变量,它可能具有整数值,因为很明显可能包含一些整数。 在那里

    $this->uri->segment();
    

    回报你:

    product_id=id
    quantity=qty
    ajax=1
    

    然后你通过调用获取值 2,即数量

    $uri2 = $this->uri->segment(2); 
    

    它会返回数量给你,而你没有在你的代码中定义一个 index() 函数,它会给出 500 错误

    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();
       }
     }
    

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 2015-06-06
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2022-08-08
      • 1970-01-01
      • 2018-02-21
      相关资源
      最近更新 更多