duanqian2368 2013-09-10 22:10
浏览 69
已采纳

jQuery ajax返回意外的数据类型

attempting to implement some server side error checking for input in the very useful "handsontable" jscript library.

the following call works great:

jQuery.ajax({
    url: "index.php?option=com_catalogscontroller=batchsave",
    data: {"formData": querydata.getData().splice( 0, 1 ) },
    dataType: 'JSON',
type: 'POST',
    success: function ( response ) {
    if( response.success ) {
    } else {
    querydata.loadData( response.data );
    }
}
});

I thought the most effective way to do error checking is on the server (PHP) side, to create a multidimensional array to track any errors discovered by the server and then return that array with the form data in the same ajax call. So I modified the server code to return both the form data and the error array back to the javascript ajax call. i.e.:

if( !empty( $errors ) ) // $errors is an multi dimensional array same size as the form data
    $result['success'] = false;
    $result['msg'] = 'There were errors detected';
    $result['data']['form'] = $formData;
    $result['data']['errors'] = $errors;
}
echo json_encode( $result );

and then on the client side, the javascript routine above has been modified to:

jQuery.ajax({
    url: "index.php?option=com_catalogscontroller=batchsave",
    data: {"formData": querydata.getData().splice( 0, 1 ) },
    dataType: 'JSON',
    type: 'POST',
    success: function ( response ) {
    if( response.success ) {
    } else {
            formErrors = response.data.errors; // formErrors is a global
    querydata.loadData( response.data.form );
    }
}
});

The original function of the form is preserved (the form data is retrieved and properly inserted in the html), but formErrors returns with a result to me that is baffling. An alert immediately after the assignment 'alert( formErrors )' shows something like a list:

true,true,false,true,true

and I can also alert on a specific index without problem e.g. alert( formErrors[0][2] ); would display 'false'. But outside of the ajax call, the array seems to be inaccessible, giving 'undefined' errors. And both in the ajax call, and in routines outside of the ajax call alert( typeof formErrors ) displays 'object' and alert( formErrors) gives the same comma list as above, but I don't want an object, I am expecting an array OR i'd be happy with an object as long as i could access it by indices. What am I missing here?

  • 写回答

1条回答 默认 最新

  • duandai0373 2013-09-12 04:09
    关注

    The problem I was having appear to be centered around JSONing.

    Most docs identify the requirement to JSON variables in php routines supporting a Javascript AJAX call. The use of the jQuery.ajax call alleviates some of the requirement, but if you don't know what you're doing (like me), its easy to get in trouble.

    My php routine JSON encodes the complete response record with the statement:

    return json_encode( $result );
    

    Because of my:

    dataType: JSON
    

    parameter in the jQuery.ajax() call, this results in an automatic json.parse() of the result returned by the PHP routine to the jQuery javascript function. This is not successful however because the php json_encode call in the server code is not recursive, so while the result array is decoded, any arrays within that result are not.

    the solution then is to json encode the components of the multidimensional array and then decode them on the client side. e.g.

    if( !empty( $errors ) ) 
        $result['success'] = false;
        $result['msg'] = 'There were errors detected';
        $result['data']['form'] = json_encode( $formData );
        $result['data']['errors'] = json_encode( $errors );
    }
    echo json_encode( $result );
    

    And then on the client side, parse (decode) these arrays specifically:

    jQuery.ajax({
        url: "index.php?option=com_catalogscontroller=batchsave",
        data: {"formData": querydata.getData().splice( 0, 1 ) },
        dataType: 'JSON',
        type: 'POST',
        success: function ( response ) {
            if( response.success ) {
            } else {
                formErrors = JSON.parse( response.data.errors ); // formErrors is a global
                querydata.loadData( JSON.parse( response.data.form ) );
            }
        }
    });
    

    I confess freely that I don't really know what I'm doing, but the code structured above seems to make sense for the logic I've developed to explain it AND it is working for me. Anyway, thanks again to Nathan and pdoherty926.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。