Laravel 4.2 jquery object to controller

here will be my first question. I hope u will not get angry:) I have this code, which is working for me:

$('body').on('submit', '#add_tarrif_form', function(e) {
  e.preventDefault();
  var selectedItems = $('#createtarif').find(":selected"); 

  jsonObj = [];
  $.each(selectedItems, function(key, value) {
    jsonObj.push(value.value);
  });

  $.ajax({
    type: 'POST',
    url: '/admin/postData',
    data: {jsonObj : jsonObj},
    dataType: 'json',      
    success: function(data) {
      console.log(data);
    }, 
    error: function (xhr, ajaxOptions, error) {
      alert(xhr.status);
      alert('Error: ' + xhr.responseText);
      alert(error);
    },
  });
});

And my question is: Maybe somehow it is possible to pass selectedItems directly to controller? without this:

$.each(selectedItems, function(key, value) {
  jsonObj.push(value.value);
});

When I try something like this:

data: {data : selectedItems}

the page is crashing and nothing happens. Thanks for your help!

Answers


You can just grab the items like this:

var selectedItems = $('#createtarif').val();

Make sure you use the correct object key though. In the 'working' code you have you use 'jsonObj' and in the last example you use 'data'.


Need Your Help

C# Show Form Creates a new Form.. Not the one I designed

c# winforms forms

I have a console application made and a design of a settings windows form too. What i want to do is, show the windows form after a specific event.

XMLHttpRequest CORS to Google Cloud Storage only working in preflight request

google-chrome xmlhttprequest cors google-cloud-storage

I implemented browser based resumable uploads into Google's Cloud Storage using an XMLHttpRequest send to a server-side created resumable upload url. This works completely fine when disabling web