在jQuery serialize()或serializeArray()中向Ajax POST添加/push()值 [英] Adding/push() Values to Ajax POST in jQuery serialize() or serializeArray()

查看:78
本文介绍了在jQuery serialize()或serializeArray()中向Ajax POST添加/push()值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery

$('#speichern').live('click' , function () {
 //  [a]  var data_save = $('#form_rechn').serializeArray();
    var data_save_ser = $('#form_rechn').serialize(); //[b]
//  [a]  data_save[data_save.length] = {"name":"action","value":"save" },{"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))};
    var addintional = 'action=save&mysql=update' + '&' + 'total=' + Number($('#grandTotal').text().replace(/EUR/g, ""));//[b]
    var data_save = data_save_ser + '&' + addintional;//[b]
    $.ajax({ 
    type    : "POST",
    cache   : false,
    url     : 'invoice_new_action.php',
    data    : data_save,
     error:function (xhr, ajaxOptions, thrownError){
                alert(xhr.status);
                alert(thrownError);
     },
    success : function(data) { 
        $.fancybox(data); 
            }
    });
    });

[b]部分效果很好;但是,为什么[a]部分不起作用?这不是推: ,{"name":"total","value": [..]

The [b]-part works very well; however, why does not work the [a]-part? This is not pushed: ,{"name":"total","value": [..]

通过print_r($ _POST)的PHP Ouput

[b]-版本

Array ( [pnr_item_1] => 1 [pkt_item_1] => HostingXXL [desc_item_1] => 20GB, 1x.de [qty_item_1] => 4 [price_item_1] => 15.5 .... [action] => save [mysql] => update [total] => 62 )

[a]-版本

Array ( [pnr_item_1] => 1 [pkt_item_1] => HostingXXL [desc_item_1] => 20GB, 1x.de [qty_item_1] => 4 [price_item_1] => 15.5 .... [action] => save )

希望我的问题/问题很清楚. 最好的方法是什么? 有更好的方法来进行标识吗?

Hopefully my problem/question is clear. What is the best method? There are better methods to so id?

推荐答案

它应该看起来像这样:

$('#speichern').live('click' , function () {
    var data_save = $('#form_rechn').serializeArray();
    data_save.push({ name: "action", value: "save" });
    data_save.push({ name: "mysql", value: "update" });
    data_save.push({ name: "total", value: Number($('#grandTotal').text().replace(/EUR/g, "")) });
    $.ajax({ 
      type    : "POST",
      cache   : false,
      url     : 'invoice_new_action.php',
      data    : data_save,
      error   : function (xhr, ajaxOptions, thrownError){
         alert(xhr.status);
         alert(thrownError);
      },
      success : function(data) { 
         $.fancybox(data); 
      }
    });
});

要推送到数组的是{name: "name", value: "value"}形式的对象,则它们将被正确序列化/编码.选项[a](更正后的形式)通常比选项[b] 好很多,因为[b]不是属性编码的,一旦其中任何无效字符滑入该位置都会失败.弄乱你的变量.在这种情况下,因为您可以控制附加的内容,所以很安全...但是最好走总能起作用的路线:永远不要直接将data参数创建为字符串.

What you want to push onto the array are objects in the form of {name: "name", value: "value"}, then they'll be serialized/encoded correctly. Option [a] (the corrected form of it) is generally much better than option [b], since [b] isn't property encoded, and will fail the moment any invalid character slips in there to mess up your variables. In this case, because you're in control of the appended content, you're safe...but it's best to go the route that always works: never creating your data argument as a string directly.

关于为什么[a]不起作用的原因:

As for the why [a] doesn't work:

data_save[data_save.length] = {"name":"action","value":"save" },{"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))};

这是无效的,您无法一次分配2件事,您要么需要这样做:

This isn't valid, you can't assign 2 things at once, you either need to do it like this:

data_save[data_save.length] = {"name":"action","value":"save" };
data_save[data_save.length] = {"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))};

或此方法(我在上面使用的首选方法):

or this (my preferred method, as used above):

data_save.push({"name":"action","value":"save" });
data_save.push({"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))});

....或者使用 $.merge() (有点浪费,但是看起来更干净),像这样:

....or, use $.merge() (a bit more wasteful, but cleaner looking), like this:

$.merge(data_save, [{"name":"action","value":"save" }, {"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))}]);

这篇关于在jQuery serialize()或serializeArray()中向Ajax POST添加/push()值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆