2017-05-16 20 views
1

我得到兩個變量在我的jQuery的功能以及如何通過它在我的數據AJAX調用內部並把它在laravel控制器傳兩人在jQuery函數的AJAX數據調用內部變量

這是我的功能

$('#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); 
     } 
     }); 

我在這裏有兩個變量一個videosmyData現在我的問題是,如何通過數據這兩個變量和laravel控制器

+1

把一個逗號後' 'myData':myData' – RST

+0

你可以把視頻放在myData下。 – Webinion

+0

@PandhiBhaumik當我給這樣的它顯示我一個錯誤'未捕獲的類型錯誤:非法調用' – Karthiga

回答

0

你所做的一切,以及請求該變量,但忘了寫逗號

$.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) { 
     // ......... 
    } 
    }); 

順便說一句,不花時間去每個輸入定義變量,使用jquery serializePHP unserialize,或者你可以用下面這段代碼創建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我給了逗號和嘗試,但提交表單後,它顯示我的錯誤,非法調用 – Karthiga

+0

@Karthiga你可以更新的問題和寫html

? –