Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The following example demonstrates an HTML5 form saving data via an Ajax API call using the POST method. The ajax call will be processed by an REST resource written in xml.

...

Javascript ajax to resource form.xml



Code Block
languagejs
linenumberstrue
//get the form from DOM (Document object model) 
    var form = document.getElementById('x-login-form');
    form.onsubmit = function(event){
        var xhr = new XMLHttpRequest();
        var data = new FormData(form);
        //open the request
        xhr.open('POST','http://localhost:7000/tests/v1.0/form')
        //send the form data
        xhr.send(data);
        //Dont submit the form.
        return false; 
    }


On the Network


The form-data format for sending data over the network to the server.

...

The parameters sent in the POST request are received and replaced with their corresponding $ symbols. These $ variables must match the exact names of the POST params that are sent in the request. Here's the resource form.xml



Code Block
languagexml
linenumberstrue

<?xml version="1.0" encoding="UTF-8" ?>
<Resource xmlns="http://xml.metamug.net/resource/1.0" v="1.0">
    <Request method="POST">
        <Desc> Create a User </Desc>
        <Sql> 
            INSERT INTO USER (user, pass, comment) 
            values ($iuser, $xpass, $comment)
        </Sql>
    </Request>
</Resource>





In the Database

If you query the user table, you can find the data just saved from the form.

...