为什么Jquery发布不发送数据?

时间:2014-05-27 11:18:36

标签: jquery ajax jsp

嗯,这是非常奇怪的问题。我正在形成对象,然后将其发送到服务器,但数据不会到来。 (至少jsp getParameter返回null)。

  var _formData = {
      "u_id": user.userId,
      "f_id": user.filialId,
      "photo_id": photo_id
    };

    jQuery.ajax({
      url: 'rep/product_photo/product_photo_delete.jsp',
      cache: false,
      async: false,
      data: _formData,
      type: 'POST',
      success: function(data){            
        running = false;
      },
      error: function(xhr) {
        running = false;
      }
    });

并且在服务器端product_photo_delete.jsp没有获取参数?但是,如果我将其作为GET发送,那么一切都还可以。 (我已经做了很多次这样的事情,他们都在工作,但这真的很奇怪。)

  String filialId = request.getParameter("f_id");
  String userId = request.getParameter("u_id");
  String photoId = request.getParameter("photo_id");

所有这些都返回null!

此外,请求有效负载是:

u_id=0&f_id=0&photo_id=43

1 个答案:

答案 0 :(得分:3)

您似乎拥有全局ajax设置,可将contentType设置为application/json或其他内容。

问题:Demo - 查看网络选项卡以监控请求格式,而不是FormData,请求值作为请求有效负载发送

enter image description here

要将数据作为请求参数发送,请将其设置为application/x-www-form-urlencoded; charset=UTF-8,如

jQuery.ajax({
    url: 'rep/product_photo/product_photo_delete.jsp',
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    //processData:true,
    cache: false,
    async: false,
    data: _formData,
    type: 'POST',
    success: function (data) {
        running = false;
    },
    error: function (xhr) {
        running = false;
    }
});

演示:Fiddle enter image description here

相关问题