如何将formdata对象发布到ajax post调用? [英] How to post a formdata object into ajax post call ?

查看:85
本文介绍了如何将formdata对象发布到ajax post调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个formdata对象如何使用ajax帖子发布它?我试过这样的方式:



但是当我在浏览器中运行它时,它会启动一个无限循环,不知道为什么

  var  formData = {name:  ravi,年龄:  31};  //  数组 

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

$( #div3)。html(数据);

// 数据 - 来自服务器的响应
},
错误: function (jqXHR,textStatus,errorThrown){

}
} );





我的尝试:



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



这是一个新的 - 由Afzaal Ahmad Zeeshan [ / edit]

解决方案

.ajax({
url: ./ Index
类型: POST
data:formData,
success: function (data,textStatus,jqXHR){


#div3)。html(data);

// 数据 - 来自服务器的响应
},
错误: function (jqXHR,textStatus,errorThrown){

}
});





我的尝试:



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



这是一个新的 - 由Afzaal Ahmad Zeeshan [ / edit]


因为将变量命名为formData不会使其成为formdata对象。以下是否为新的整数值?

  var  integer = '  Afzaal Ahmad Zeeshan'; 



不,你也这样做,你需要的是使用 FormData 对象;不是一个普通的JavaScript对象。我写了一篇涵盖这个主题的文章,请仔细阅读,上传文件 - HTML5和jQuery Way! [ ^ ]



您需要的是一个HTML表单,然后您可以创建一个新的 FormData 来自它的对象并将其上传到服务器,示例,

 <  < span class =code-leadattribute> form     method   = 发布    id   =  formid >  
<! - 这里控制。 - >
< / form >



那么JavaScript就是,

  var  formData =  new  FormData(

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

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]

解决方案

.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]


Because naming a variable as formData doesn't make it a formdata object. Does the following make a new integer value?

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,

<form method="post" id="formid">
   <!-- Controls here. -->
</form>


Then the JavaScript would be,

var formData = new FormData(


这篇关于如何将formdata对象发布到ajax post调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆