dongtan7418
2012-05-21 17:22
浏览 65
已采纳

无法访问AJAX调用中的JSON字段

I'm new to Php/Jquery Programming, and i past all this day trying figuring how to access to a JSON field from a Jquery AJAX call:

I'm using codeingniter, and in my controller the related code is:

if( $this->input->is_ajax_request()) 
{
    $view_data['test'] = 'ajax';
    echo json_encode( $view_data ); 
}
else
{
    $view_data['test'] = 'not ajax';                              
    $data = array(
            'javascripts' => array(
            'js/jquery.dataTables.js',
            'js/zona/dataTable_zone.js',
            'js/zona/gestisci_zone.js',
            'js/jquery-ui-1.8.20.custom.min.js' ,
            'js/jquery.ui.error-panel.js',  
            'js/jquery.validate.min.js' ,
            'js/localization/messages_it.js'
        ),
        'style_sheets' => array(
            'css/south-street/jquery-ui-1.8.20.custom.css' => 'screen',                 
            'css/jquery.dataTables.css' => 'screen'
        ),
        'content' => $this->load->view( 'zona/gestisci_zone', $view_data, TRUE )
    );

    $this->load->view( $this->template, $data);
} 

` In the view the related code is

<div id="id_test">  <p><?php echo $test ?> </p></div>

and in the javascript file the related AJAX code is

`$.ajax( { type:"POST", url:this_url, dataType: 'json', data: post_data,
success: function(data){

                                             console.log(data);
                                             console.log($("id_test").empty());
                                        $("#id_test").empty().append(data.test);
                                        },
                                        error: function(data){

                                        alert("error");

);`

in this way the AJAX output is Array{"test":"ajax"}

my expected behavior is that using $("#id_test").empty().append(data.test); the AJAX output will be just the word ajax, but it just return a blank div. I've tried to access in many modes but i didnt figured how to accomplish this. Maybe i'm missing something because of my lack of experience or i'm using a wrong approach. Any advice please? :)

After a little of debug i've arrived to conclusion that is a plaintext / false JSON problem. it is returning just the text rapresentation of the array and not a json array. Now, how i can send the correct json array from controller? (i already forced mimetype to application/json with no effects)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duanchai0028 2012-05-21 17:32
    已采纳

    It's dataType and not datatype. I believe that's the problem, seems like it's not parsing the returned data as json and as a string instead.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doukui2011 2012-05-21 17:38

    I think you may need to toggle between json and jsonp if still you do not succeed. jQuery has 2 options "json" and "jsonp"

    评论
    解决 无用
    打赏 举报
  • doujinge9648 2012-05-21 17:42

    Try to add an error handler in the ajax call so you can tell whether it is executed successfully or not. And I think you have to use data.d

    评论
    解决 无用
    打赏 举报
  • dongxiongshi9952 2012-05-21 20:55

    just a guess but... syntax error maybe?

    if( $this->input->is_ajax_request()) 
    {
        view_data['test'] = 'ajax';
        // dont you mean $view_data?
    
        echo json_encode( $view_data ); 
    }
    
    评论
    解决 无用
    打赏 举报
  • dongwei5740 2012-05-22 08:14

    If I understand you correctly, in order for your <div id="id_test"> <p><?php echo $test ?> </p></div> to be replaced by the word "ajax" from $.ajax(), try this:

    $.ajax({
      type:"POST",
      url:this_url, 
      dataType: 'json',
      data: post_data, 
      success: function(data){
        $("#id_test").empty().append(data.test);
      }
    });
    

    Since you mention that the data returns Array{"test":"ajax"}, you can access the returned JavaScript object in this manner data.test

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题