Breaking News

jquery – multiple select field pass ajax data as string

Questions:

My problem is the quantity value pass as a string not as an array. like ["1,2"], however what I need is [1,2]

// get the quantity value:
if (res) {
  $("#quantity").empty();
  $.each(res, function(key, value) {
    $("#quantity").append('');
  });
} else {
  $("#quantity").empty();
}

// pass the quantity value:
var quantity = new Array(); //storing the selected values inside an array
$('#quantity :selected').each(function(i, selected) {
  quantity[i] = $(selected).val();
});

$.ajax({
  type: "POST",
  url: "{{route('localBook.store')}}?quantity[]=" + quantity,
  dataType: 'json',
  data: form_data,
  success: function(res) {}
});

How to&Answer:

I think this happens because you are passing the quantity-array as a url parameter instead of passing it in the http body (as intended in the post request). This is redundant if you want to perform a POST-request.

Edit your request, pass data through the data-field in your ajax request and everything will work fine. 🙂

$.ajax({
  type: "POST",
  url: "{{route('localBook.store')}}",
  dataType: 'json',
  data: {
    quantity: this.quantity
  },
  success: function(res) {}
});

Cheers,

Niklas

About rankhawks

Check Also

jQuery replace image src value if has specific character

Questions: I have found this code but it’s not working. jquery $("#comments.comments .avatar-image-container img[src*='SA=s85']").attr("src","https://rawcdn.githack.com/xysrgithub/blogger/208593eae3ef67c7320246e1a80c6d97656e28dc/img/mementomag/anonymous.png").ssyby("blank"); I …

css – Jquery DataTables (1.10) render extra footer when verticle scrollbar appears (overflow-y: auto)

Questions: I am using datatables with Bootstrap 4 and everything was working fine until I …

jQuery: set active class on selected option

Questions: I’m trying to add the “active” class in an option element but this code …

javascript – Request to server with $.ajax doesn’t return success – PHP

Questions: I’m trying to make a request to server on a with jQuery and ajax …

python – How to select rows from a DataFrame based on column values?

How to select rows from a DataFrame based on values in some column in Python …

Leave a Reply

Your email address will not be published. Required fields are marked *