【问题标题】:pass two variable inside ajax data call in jquery function在 jquery 函数中的 ajax 数据调用中传递两个变量
【发布时间】:2017-05-16 12:43:28
【问题描述】:

我在我的 jquery 函数中获得了两个变量,以及我如何在 ajax 调用中将它传递到我的数据中并在 laravel 控制器中获取它

这是我的功能

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

这里我有两个变量一个 videos 和另一个 myData 现在我的问题是如何在数据中传递这两个变量并在 laravel 控制器中请求这个变量

【问题讨论】:

  • 'myData':myData后面加逗号
  • 你可以自己把视频放在myData下。
  • @PandhiBhaumik 当我这样给出时,它会显示一个错误Uncaught TypeError: Illegal invocation

标签: javascript php jquery ajax laravel-5.4


【解决方案1】:

你已经做得很好了,但是忘记写逗号

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

【讨论】:

  • @lennon 我给了逗号并尝试了,但在提交表单后它显示了一个错误,即非法调用
  • @Karthiga 你能更新问题并写html
    吗?
猜你喜欢
  • 2011-06-07
  • 2012-03-08
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多