2016-09-11 37 views
1

嗨我在我的項目中使用https://github.com/getmeuk/ContentTools庫,在文檔中使用普通的js發送ajax請求,我想用jquery發送數據,但我不知道如何那。如何通過jQuery在getcontenttools庫中發送ajax

這是在普通的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); 

回答

1

使用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); 
}); 
+0

非常感謝你 –