在JS中通过XMLHttpRequest对象发布formdata? (跨浏览器) [英] Post formdata via XMLHttpRequest object in JS ? ( cross browser)

查看:338
本文介绍了在JS中通过XMLHttpRequest对象发布formdata? (跨浏览器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在js中发布表单数据:



我有这个代码:

  var formData = new FormData(); 
formData.append(username,Groucho);
formData.append(accountnum,123456)
formData.append(afile,2);

var xhr = new XMLHttpRequest();
xhr.open(POST,http://xxxxx/xx.ashx,true);
xhr.send(formData);

Formdata根据)



跨浏览器的AJAX文件上传是很难做的。您可以尝试在Javascript中建立自己的表单标头/边界(请参阅这里的解答: XMLHttpRequest POST multipart / form-data ),但是,个人而言,我不相信这是值得的。


Im trying to post a form data in js :

I have this code :

var formData = new FormData();
  formData.append("username", "Groucho");
  formData.append("accountnum", 123456);
  formData.append("afile", "2");

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://xxxxx/xx.ashx",true);
xhr.send(formData);

Formdata according MDN is not available in IE ( or unknown).

When I try this in FF :

( i think its fine...).

when I try in IE :

What is the solution to post form data ( or my data but in objective way) CROSSBROWSER ?

解决方案

You didn't say which version of IE you're using. The formData object is not supported in IE9 or lower. XMLHTTPRequest2 (which contains the formData object) should be supported in IE10 (http://caniuse.com/xhr2)

Cross-browser AJAX file upload is very hard to do right now. You could try building your own form header/boundaries in Javascript (see answer here: XMLHttpRequest POST multipart/form-data) but, personally, I don't believe it is worth the effort.

这篇关于在JS中通过XMLHttpRequest对象发布formdata? (跨浏览器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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