如何通过发出包含名为“X-METHOD”的标头的POST请求来模拟AJAX调用中的PUT请求?

问题描述 投票:0回答:1

我正在尝试发送PUT请求以使用AJAX更新数据库中的名称字段。但是,如何通过模拟POST请求发送它?

ajax http post get put
1个回答
0
投票

function ajax_http_request(url, method, data, callback) {
    var header = {'If-Match': '1.0.0'};
    var token = cookie.get('token');
    if (typeof(token) == 'string' && token != '') {
        header.token = token;
    }
    if (method === 'get') {
        var page = 1, page_size = 1;
        if (data != null && !isNaN(parseInt(data.page))) {
            page = Math.max(page, parseInt(data.page));
        }
        if (data != null && !isNaN(parseInt(data.page_size))) {
            page_size = Math.max(page_size, parseInt(data.page_size));
        }
        header.range = page + '/' + page_size;
    }

    $.ajax({
        type: method.toUpperCase(),
        url: system.url + url,
        data: data,
        dataType: 'json',
        timeout: 5000,
        headers: header,
        success: function(response){
            if (response.error == 3 || response.error == 4) {
                cookie.move('token');
                location.href = '/file/iuser/login.html';
            } else {
                callback(response);
            }
        },
        error: function(xhr, type){
            popup.tip('服务异常,请稍后再试',3);
        }
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.