2017-05-16 1 views
1

ich zwei Variable bekommen in meinem Jquery Funktion und, wie ich übergeben Sie es in meine Daten innerhalb Anruf Ajax und bekommen es in Laravel ControllerPass zwei Variable in Ajax-Datenanruf in Jquery Funktion

Dies ist meine Funktion

$('#updateProduct').on('submit', function(e){ 
    e.preventDefault(e); 
    var redirect_url = $(this).find("[name='redirect_url']").val(); 
    var url = $(this).attr('action'); 
    var method = $(this).attr('method'); 
    var videos = document.getElementById('videoToUpload').files[0]; 
     var myData ={ 
     'name': $(this).find("[name='name']").val(), 
     'description': $(this).find("[name='description']").val(), 
     'brand': $(this).find("[name='brand']").val(), 
     'category': $(this).find("[name='category']").val(), 
     'condition': $(this).find("[name='condition']").val(), 
     'shipper': $(this).find("[name='shipper']").val(), 
     'shipping_from': $(this).find("[name='shipping_from']").val(), 
     'shipping_paid_by': $(this).find("[name='shipping_paid_by']").val(), 
     'shipping_within' :$(this).find("[name='shipping_within']").val(), 
     'shipping_weight': $(this).find("[name='shipping_weight']").val(), 
     'shipping_fee': $(this).find("[name='shipping_fee']").val(), 
     'seller_get' : $(this).find("[name='seller_get']").val(), 
     'price_per_unit': $(this).find("[name='price_per_unit']").val(), 
     'selling_fee' : $(this).find("[name='selling_fee']").val(), 
     'is_active':$(this).find("[name='is_active']:checked").val(), 
     //'videos' :$("#videoToUpload").files[0], 
    //'videos' : document.getElementById('videoToUpload').files[0], 
    } 
    console.log(data); 
    $.ajax({ 
     type: method, 
     url: url, 
     dataType: 'JSON', 
     data: {'myData':myData 
       'videos':new FormData("videos", document.getElementById('videoToUpload').files[0]) 
       }, 
     success: function(data){ 
      alert("Products updated successfullly"); 
      console.log(data); 
      //window.location.href = redirect_url; 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
      console.log(JSON.stringify(jqXHR)); 
      console.log("AJAX error: " + textStatus + ' : ' + errorThrown); 
     } 
     }); 

Hier bin ich mit zwei variablen eine videos und andere myData nun meine Frage ist, wie diese beiden variablen in Daten zu übergeben und fordern Sie diese Variable in Laravel Controller

+1

ein Komma erstellen setzen nach ' 'myData': myData' – RST

+0

Sie können Videos unter myData selbst platzieren. – Webinion

+0

@PandhiBhaumik wenn ich so gebe es zeigt mir einen Fehler 'Uncaught TypeError: Illegal Invocation' – Karthiga

Antwort

0

Sie alles gut gemacht, aber vergessen zu schreiben Komma

$.ajax({ 
    type: method, 
    url: url, 
    dataType: 'JSON', 
    data: {'myData': myData, 'videos': new FormData("videos", document.getElementById('videoToUpload').files[0]) }, 
    success: function(data){ 
     // ......... 
    }, 
    error: function(jqXHR, textStatus, errorThrown) { 
     // ......... 
    } 
    }); 

By the way, verbringen nicht Zeit, um jede Eingabe in Variable zu definieren, verwenden Sie jquery serialize und PHP unserialize, oder Sie können diesen Code unten verwenden Serialize Object

$.fn.serializeObject = function() { 
    var o = {}; 
    var a = this.serializeArray(); 
    $.each(a, function() { 
     if (o[this.name] !== undefined) { 
      if (!o[this.name].push) { 
       o[this.name] = [o[this.name]]; 
      } 
      o[this.name].push(this.value || ''); 
     } else { 
      o[this.name] = this.value || ''; 
     } 
    }); 
    return o; 
}; 
+0

@Lennon ich gab Komma und versuchte, aber nach dem Absenden des Formulars zeigen Sie mir einen Fehler, dass illegaler Aufruf – Karthiga

+0

@Karthiga können Sie die Frage aktualisieren und schreibe html

? –