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

查看:98
本文介绍了使用 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 表单插件,它允许您将表单和也支持文件上传.

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天全站免登陆