Tuesday, 25 November 2014

Creating form data Object

var formData = new FormData();
formData.append("username", "bujjibabu"); 
formData.append("password", 'password');
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost/register");  
xhr.send(formData);

Reference Link

we can add some more data to existing form data

var formElement = document.getElementById("Id");
var formData = new FormData(formElement);
formData.append("additionaldata", "somedata");
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.send(formData);

send the form data using jquery

var formElement = document.getElementById("Id");
var formData = new FormData(formElement);
formData.append("additionaldata", "somedata");
$.ajax({ url: "post.php", type: "POST", data: formData, processData: false, // tell jQuery not to process the data contentType: false // tell jQuery not to set contentType });

sending file using form data

var formData = new FormData();
formData.append("file", file);
var xhr = new XMLHttpRequest();
xhr.open("POST",uploadURL);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); 
xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){
        $('#output').html(xhr.responseText);
    }
}
xhr.send(formData);

No comments:

Post a Comment