Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a formdata object how do i post it using ajax post ? I tried it this way:

but when i run it in the browser , it will just start an infinite loop, not sure why
JavaScript
var formData = { name: "ravi", age: "31" }; //Array

     $.ajax({
         url: "./Index ",
         type: "POST",
         data: formData,
         success: function (data, textStatus, jqXHR) {

             $("#div3").html(data);

             //data - response from server
         },
         error: function (jqXHR, textStatus, errorThrown) {

         }
     });


What I have tried:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[edit]This is a new one — by Afzaal Ahmad Zeeshan[/edit]
Posted
Updated 4-Dec-16 1:35am
v2

1 solution

Because naming a variable as formData doesn't make it a formdata object. Does the following make a new integer value?
JavaScript
var integer = 'Afzaal Ahmad Zeeshan';

No. You're doing the same, what you need is to use FormData object; not a plain JavaScript object. I wrote an article that covers this topic, kindly read it, Uploading the Files – HTML5 and jQuery Way![^]

What you need is an HTML form, then you can create a new FormData object from it and upload it to server, sample,
HTML
<form method="post" id="formid">
   <!-- Controls here. -->
</form>

Then the JavaScript would be,
JavaScript
var formData = new FormData($('#formid')[0]);
$.ajax({
   url: 'path',
   data: formData,
   success: function(response) {
      // handler
   }
});

Read the article for more deeper overview of this.

FormData - Web APIs | MDN[^]
 
Share this answer
 
Comments
Karthik_Mahalingam 4-Dec-16 8:57am    
5
Afzaal Ahmad Zeeshan 4-Dec-16 9:16am    
Thank you, Karthik.
forte74 4-Dec-16 21:38pm    
why cant i write $("#div3").html(data); it will just start an infinite loop
Afzaal Ahmad Zeeshan 5-Dec-16 11:08am    
You can write it, but that will just write the response to HTML DOM; nothing else.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900