RustamIS
RustamIS

Reputation: 697

Why Jquery post not sending data?

Well, here is very odd problem. I'm forming object and then sending it to server, but data not coming. (at least jsp getParameter returning 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;
      }
    });

and at the server side product_photo_delete.jsp is not getting parameters? However, if I send it as GET then everything is okay. (I have done such things many times, all of them were working, but this is really Odd).

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

all of them returning null !

moreover the request payload is:

u_id=0&f_id=0&photo_id=43

Upvotes: 1

Views: 3109

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Looks like you have a global ajax setting, which sets contentType to application/json or something else.

Problem: Demo - Look at the network tab to monitor the request format, instead of FormData, the request values are sent as request payload

enter image description here

To sent the data as request parameters, set it to application/x-www-form-urlencoded; charset=UTF-8 like

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;
    }
});

Demo: Fiddle enter image description here

Upvotes: 3

Related Questions