HTTP Requests

The XMLHttpRequest object allows the user to register a response handle function and a url. A request can be send using one of the http verbs (get, post, put, delete, ...) to make the request. When the response arrive the handle function is called. The handle function is called several times. Every-time the request state has changed (for example headers have arrived or request is done).

Here a short example:

function request() {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
            print('HEADERS_RECEIVED');
        } else if(xhr.readyState === XMLHttpRequest.DONE) {
            print('DONE');
        }
    }
    xhr.open("GET", "http://example.com");
    xhr.send();
}

Read more:

Examples

Zapier

Using Zapier Webhooks. Download the zapier.pma example.

1_. Create new ZAP https://zapier.com/apps/email/integrations/webhook/62/turn-webhooks-into-sent-emails

2_. Add following fields

3_. Edit Template

4_. Turn on your ZAP

5_.Paste Webhook URL into code

The code:

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {

    if(xhr.readyState === XMLHttpRequest.DONE) {
        console.log("Status code is: " + xhr.status + " (" + xhr.statusText + ")")
        if (xhr.status == 200) {
            console.log("responseText: ")
            console.log(xhr.responseText.toString())            
            var json = JSON.parse(xhr.responseText.toString())
            console.log("json.status: ")
            console.log(json.status)
        }
    }
}

xhr.open("POST", "https://hooks.zapier.com/hooks/catch/YYYYYY/ZZZZZZ/");

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var data =  "email="+encodeURIComponent("email@example.org") + 
            "&text="+encodeURIComponent("Message body text") + 
            "&subject="+encodeURIComponent("Email subject")

xhr.send(data)

Mailgun

Send email with mailgun. Download the mailgun.pma example.

1_. Open domain info in mailgun dashboard

2_. Use the following code to send email. Replace it with your Domain and API Key:

function request() {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {        

        if(xhr.readyState === XMLHttpRequest.DONE) {
            console.log("Status code is: " + xhr.status + " (" + xhr.statusText + ")")

            if (xhr.status == 200) {
                console.log("responseText: ")
                console.log(xhr.responseText.toString())

                var json = JSON.parse(xhr.responseText.toString())
                console.log("json.message: ")
                console.log(json.message)
            }            
        }
    }

    xhr.open("POST", "https://api.mailgun.net/v3/sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org/messages");

    xhr.setRequestHeader('Authorization', "Basic " + Qt.btoa("api:key-YYYYYYYYYYYYYYYYYYYYYYYYY"));
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    var data =  "from="+encodeURIComponent("email@example.org") + 
                "&to="+encodeURIComponent("another@example.org") + 
                "&subject="+encodeURIComponent("Subject message") + 
                "&text="+encodeURIComponent("Message body text")

    xhr.send(data)
}

results matching ""

    No results matching ""