【问题标题】:Codeigniter json encoded data retrived using AJAX not accessible in php使用 AJAX 检索的 Codeigniter json 编码数据在 php 中无法访问
【发布时间】:2017-06-10 08:56:10
【问题描述】:

我正在尝试在其中 AJAX 自动完成的 codeigniter 文件中进行实时搜索

     $(this).ready( function() 
    {
        $("#id").autocomplete
        ({
          minLength: 1,
          source: 
          function(req, add){
              $.ajax({
                  url:'<?php echo site_url('autocomplete/lookup'); ?>',
                  dataType: 'json',
                  type: 'POST',
                  data: req,
                  success:    
                  function(data){
                      if(data.response =="true"){
                          add(data.message); // displays the retrieved data
                        }
                    },

                });
            },     
        });
    });

自动完成控制器的查找功能是

public function lookup(){
    // process posted form data
    //echo $this->input->post('term'); exit;
    $keyword = $this->input->post('term');

    $data['response'] = 'false'; //Set default response
    $query = $this->MAutocomplete->lookup($keyword); //Search DB
    if( ! empty($query) )
    {
        $data['response'] = 'true'; //Set response
        $data['message'] = array(); //Create array
        foreach( $query as $row )
        {
            $data['message'][] = array( 
                                    'id'=>$row->id,
                                    'value' => $row->firstname,

                                 );  //Add a row to array
        }
    }
    if('IS_AJAX')
    {

        echo json_encode($data); //echo json string if ajax request


    }
    else
    {
        $this->load->view('data_view',$data); //Load html view of search results
    }
}

我可以使用视图代码中的 add(data.message) 访问以 json 编码形式返回的数据。 我无法在 ajax 代码之外访问这些数据,我应该怎么做才能让我在我的 php 代码中使用这个返回的数据。

【问题讨论】:

  • console.log(data)放到你的成功函数中。不要提醒它
  • @NishantNair 我没有使用警报,我使用了 add 将检索到的数据添加到下拉列表中
  • 抱歉,我看错了。 add 方法在做什么?实施在哪里?
  • @NishantNair 它是 jquery 的一部分,可能是因为 add 函数是我读到的代码的一部分,唯一提供的解释是它显示了数据
  • 您是否尝试过使用以下答案。您应该在使用前对数据进行解码

标签: php jquery json ajax codeigniter


【解决方案1】:

我设法解决了我的问题,因为我发现由于数据类型是 JSON,所以我不需要解析 JSON。

只是做

obj = data.message;
console.log(obj);

This shows the JSON being displayed in the console

【讨论】:

    【解决方案2】:

    代替:

    if('IS_AJAX')
    

    我建议(使用输入类)

    if ($this->input->is_ajax_request()) {
       echo json_encode($data);
    }
    

    这使用表单帖子中的 HEAD 详细信息来确定请求是否是 xhrrequest

    【讨论】:

      【解决方案3】:

      我还可以看到数据被编码成 json。所以需要在js中解码。

        success:    
            function(data){
              var obj = jQuery.parseJSON(data); // if using jquery
      
               console.log(obj.message); // check your data and the use it accordingly.
            },
      

      在你的控制器中

      $isAJAX     = $this->input->post('isAjax');
      if($isAJAX)
      {
          echo json_encode($data); //echo json string if ajax request
      
      }
      else
      {
          $this->load->view('data_view',$data); //Load html view of search results
      }
      

      【讨论】:

      • 当我使用此代码代替 add 函数时,出现错误 SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
      • 如果你使用 jquery 试试jQuery.parseJSON(data);
      • 我已经使用了这段代码,这就是我在上面评论中提到的错误的原因。还有其他为什么我可以显示这个 obj 来检查 JSON 是否正在被解析
      • 您的代码似乎没有进入 if $isAJAX = $this-&gt;input-&gt;post('isAjax'); 在 if 条件下尝试 $isAjax
      • 如果你使用dataType: 'json'并且返回的数据实际上是JSON,则无需解析数据
      猜你喜欢
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      相关资源
      最近更新 更多