jQuery load方法替代和错误调试

115 阅读1分钟

load方法适合快速加载页面,如果需要调试错误信息,则不是那么方便。

load方式(不推荐):

$("#right_content").load('jingdian_update/id/' + id + '/page/' + update_page)

ajax.fail方式:

    $.ajax({
        url: 'jingdian_update/id/' + id + '/page/' + update_page,
        method: 'GET',
        dataType: 'html',
        success: function (response) {
            console.log('请求成功:', response);
            $("#right_content").html(response);
        }
    }).fail(
        function (error) {
            ajax_error(error);
        }
    )

好处:使用ajax方法后可以使用fail方法进行错误调试。

或者使用完整的error参数:

$.ajax({
    url: 'jingdian_update/id/' + id + '/page/' + update_page,
    method: 'GET',
    dataType: 'html',
    success: function (response) {
        console.log('请求成功:', response);
        $("#right_content").html(response);
        // 处理响应数据
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.error('请求失败:', textStatus, errorThrown);
        console.error('状态码:', jqXHR.status);
    },
    complete: function () {
        console.log('请求完成');
    }
});