使用jQuery使用multipart / form-data进行HTTP POST调用? [英] Making an HTTP POST call with multipart/form-data using jQuery?

查看:1203
本文介绍了使用jQuery使用multipart / form-data进行HTTP POST调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery进行带有multipart / form-data的HTTP POST调用:

I'm trying to make a HTTP POST call with multipart/form-data , using jQuery:

$.ajax({
  url: 'http://localhost:8080/dcs/rest',
  type: 'POST',
  contentType:'multipart/form-data',
  data: 'dcs.source=boss-web&query=data&dcs.algorithm=lingo&dcs.output.format=JSON&dcs.clusters.only=true', 
  //dataType: "jsonP",
  success: function(jsonData) {alert('POST alert'); data=jsonData ; },
  error : function(XMLHttpRequest, textStatus, errorThrown) {
            console.log('An Ajax error was thrown.');
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
          }
});

它不起作用。 Firebug返回一个未定义的错误,返回的 XMLHttpRequst 对象多部分字段设置为false。

It doesn't work. Firebug returns an undefined error and the returned XMLHttpRequst object multipart field is set to false.

我可以做些什么来使用jQuery?如果不可能有一个简单的实现这个?

What can i do to make this work with jQuery? And if it's not possible is there a simple to achieve this?

即。不需要传输文件,只需要一些数据。但服务器需要多部分。

i.e. idon't need to transfer files , just some data. but the server requires multipart.

推荐答案

multipart / form-data 不看起来像这样:

dcs.source=boss-web&query=data&dcs.algorithm=lingo&dcs.output.format=JSON&dcs.clusters.only=true

这是 application / x-www-form-urlencoded

这是示例 multipart / form-data 请求的样子。以及相关的 RFC 1867

Here's an example of how multipart/form-data request looks like. And the related RFC 1867.

multipart / form-data 经常与上传文件相关联。如果是这种情况,你可以看看 jquery表单插件,它允许你ajaxify表单和也支持文件上传

multipart/form-data is quite often associated with uploading files. If this is your case you could take a look at the jquery form plugin which allows you to ajaxify forms and supports file uploads as well.

这篇关于使用jQuery使用multipart / form-data进行HTTP POST调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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