ali ali - 1 month ago 6
jQuery Question

How To Send FormData object?

1.get Form with id
2.create formdata with Form ID
3.send Data with $.ajax

<form id="company">
<input type="text" name="name" />
<input type="tel" name="tel" />
<input type="button" id="send" name="send" value="send" />
</form>





<script>
##get Form id an create form data##
var testForm = document.getElementById('send');
testForm.onclick = function(event) {
var formData = new FormData(document.getElementById('company'));
##this is Ajax Method##
$.ajax({
url : './json/company.php',
method : 'POST',
data : formData,
timeout : 10,
dataType :'json',
success: function(data)
{
alert("Success");
}
});
}
</script>


Error : TypeError: Argument 1 of FormData.constructor is not an object


Answer

Since your form doesn't have a input type file you should use the serialize function instead

$(testForm).click(function(event) {

                $.ajax({
                    url : './json/company.php',
                    method : 'POST',
                    data : $('#company').serialize(),
                    timeout : 10,
                    dataType :'json',
                    success: function(data)
                    {
                        alert("Success");
                    }
                });
        });
Comments