【问题标题】:JQuery Ajax POST in CodeigniterCodeigniter 中的 JQuery Ajax POST
【发布时间】:2013-07-11 10:11:50
【问题描述】:

我已经搜索了很多关于 POST 方法的教程,也在这里看到了已回答的问题,但我的 POST 仍然无法正常工作……我想如果你们看到我没有看到的东西,我应该把它贴在这里!

我的 js -messages.js:

$(document).ready(function(){   

    $("#send").click(function()
    {       
     $.ajax({
         type: "POST",
         url: base_url + "chat/post_action", 
         data: {textbox: $("#textbox").val()},
         dataType: "text",  
         cache:false,
         success: 
              function(data){
                alert(data);  //as a debugging message.
              }

     return false;
 });
 });

我的看法 - chat.php:

<?php $this->load->js(base_url().'themes/chat/js/messages.js');?> //i use mainframe framework which loading script this way is valid



<form method="post">
    <input id="textbox" type="text" name="textbox">
    <input id="send" type="submit" name="send" value="Send">
</form>

最后一个我的控制器 - chat.php

//more functions here

function post_action()
{   
    if($_POST['textbox'] == "")
    {
        $message = "You can't send empty text";
    }
    else
    {
        $message = $_POST['textbox'];
    }
    echo $message;
}

【问题讨论】:

  • 您是否遇到任何错误..请检查您的开发者工具控制台
  • 它会去控制器吗?它会抛出什么错误?
  • 你在哪里定义url: base_url,我相信这是你的问题。
  • 我在配置文件中定义了我的 url,然后用框架我加载了这个: var base_url = '';
  • 不,我在控制器中使用了 print_r,但我什么也没得到……也没有错误……我点击发送,然后页面就刷新了

标签: codeigniter jquery post


【解决方案1】:
$(document).ready(function(){   

    $("#send").click(function()
    {       
     $.ajax({
         type: "POST",
         url: base_url + "chat/post_action", 
         data: {textbox: $("#textbox").val()},
         dataType: "text",  
         cache:false,
         success: 
              function(data){
                alert(data);  //as a debugging message.
              }
          });// you have missed this bracket
     return false;
 });
 });

【讨论】:

  • 因为你错过了括号。 javascript只是刷新
  • 是的,非常感谢这是我的问题...我找了好几个小时!
  • 控制器返回 json 编码的对象,我怎样才能把这个对象放入 php 脚本?,这意味着我如何将 json 对象从 ajax 成功函数传递到 php 脚本。
【解决方案2】:

在 codeigniter 中,不需要在 ajax post 方法中发送“数据”.. 这是示例。

   searchThis = 'This text will be search';
    $.ajax({
      type: "POST",
      url: "<?php echo site_url();?>/software/search/"+searchThis,
      dataType: "html",
      success:function(data){
        alert(data);
      },

    });

注意:在 url 中,software 是控制器名称,search 是函数名称,searchThis 是我发送的变量。

这是控制器。

    public function search(){
    $search = $this->uri->segment(3);
      echo '<p>'.$search.'</p>';
    }

我希望你能对你的工作有所启发。

【讨论】:

    【解决方案3】:
        <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
        class UserController extends CI_Controller {
    
            public function verifyUser()    {
                $userName =  $_POST['userName'];
                $userPassword =  $_POST['userPassword'];
                $status = array("STATUS"=>"false");
                if($userName=='admin' && $userPassword=='admin'){
                    $status = array("STATUS"=>"true");  
                }
                echo json_encode ($status) ;    
            }
        }
    
    
    function makeAjaxCall(){
        $.ajax({
            type: "post",
            url: "http://localhost/CodeIgnitorTutorial/index.php/usercontroller/verifyUser",
            cache: false,               
            data: $('#userForm').serialize(),
            success: function(json){                        
            try{        
                var obj = jQuery.parseJSON(json);
                alert( obj['STATUS']);
    
    
            }catch(e) {     
                alert('Exception while request..');
            }       
            },
            error: function(){                      
                alert('Error while request..');
            }
     });
    }
    

    【讨论】:

      【解决方案4】:

      这个问题已经得到解答,但我想我也会让你知道,我建议你使用CodeIgniter input class,而不是使用原生 PHP $_POST,这样你的控制器代码就可以了

      function post_action()
      {   
          if($this->input->post('textbox') == "")
          {
              $message = "You can't send empty text";
          }
          else
          {
              $message = $this->input->post('textbox');
          }
          echo $message;
      }
      

      【讨论】:

      • 如果您能解释一下为什么应该使用 codeigniter 输入类会很有用,即它比 $_POST 有什么优势?
      • 使用 CI 输入类的好处是包含了自动 xss_filtering(除非在配置中禁用)
      【解决方案5】:
      <script>
      $("#editTest23").click(function () {
      
              var test_date = $(this).data('id');
              // alert(status_id);    
      
              $.ajax({
                  type: "POST",
                  url: base_url+"Doctor/getTestData",
                  data: {
                      test_data: test_date,
                  },
                  dataType: "text",
                  success: function (data) {
                      $('#prepend_here_test1').html(data);
                  }
              });
              // you have missed this bracket
              return false;
          });
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多