'How to translate ajax to GM_xmlHttpRequest?

I'm trying to translate the ajax request to GM_xmlHttpRequest, but I can't find any info on how to do the jquery ajax beforeSend, success, error, complete.

$.ajax({
    url: url,
    type: 'GET',
    dataType: 'json',
    timeout: 30000,
    beforeSend: function(){
        // some code
    },
    success: function(res) {
        console.log(res);
    },
    error: function(res){
        console.log("Failed to fetch data");
    },
    complete: function(res){
        console.log("xhr completed");
        // some code
    }
});

How can I translate this to GM_xmlHttpRequest? All examples I found use only onload in the gm xhr. How can I have success, error, complete?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source