javascript - 如何在getcontenttools库中通过jquery发送ajax

标签 javascript jquery ajax

嗨,我正在使用https://github.com/getmeuk/ContentTools我的项目中的库,在文档中使用纯js发送ajax请求,我想使用jquery发送数据, 但我不知道该怎么做。

这是用纯 JavaScript 发送 ajax 的代码:

// Send the update content to the server to be saved
    onStateChange = function(ev) {
        // Check if the request is finished
        if (ev.target.readyState == 4) {
            editor.busy(false);
            if (ev.target.status == '200') {
                // Save was successful, notify the user with a flash
                if (!passive) {
                    new ContentTools.FlashUI('ok');
                }
            } else {
                // Save failed, notify the user with a flash
                new ContentTools.FlashUI('no');
            }
        }
    };

    xhr = new XMLHttpRequest();
    xhr.addEventListener('readystatechange', onStateChange);
    xhr.open('POST', '/x/save-page');
    xhr.send(payload);

最佳答案

使用 JQuery 本教程的这一部分可以重写如下:

// Collect the contents of each region into an object we can send to the 
// server.
var payload = {'__page__': window.location.pathname};
for (var name in regions) {
    payload[name] = regions[name];
}

// Send the updated content to the server to be saved
var req = $.ajax({
    method: 'POST',
    url: '/x/save-page',
    data: payload
    });

req.done(function() {
    // Save was successful, notify the user with a flash
    new ContentTools.FlashUI('ok');
});

req.fail(function() {
    // Save failed, notify the user with a flash
    new ContentTools.FlashUI('no');
});

req.always(function() {
    // Make sure the editor is no longer set in a busy state
    ContentTools.EditorApp.get().busy(false);
});

关于javascript - 如何在getcontenttools库中通过jquery发送ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39433569/

相关文章:

jquery - AJAX Jsonp 调用在 IE9 中失败

c# - 如何检测客户端是否可以访问 iFrame 中的网站?

javascript - 将工具提示箭头定位在左侧而不是底部

javascript - 如何将 HTML 文本插入 jQuery

javascript - 复制 JavaScript 数组

javascript - 如何获取动态创建的选择框的值

javascript - 使用$route获取URL参数

javascript - 网站内容在褪色的标题上滚动

jquery - 如何在 django 中序列化字典以在 Jquery 中呈现 [问题级别 - 初学者]

javascript - 限制向表动态添加的列