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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog