jquery ajax post serialize,javascript - jQuery .serialize() for post? - Stack Overflow

博客讨论了如何在Ajax请求中同时传递表单数据和其他额外变量。由于不能直接使用jQuery的serialize()方法,作者考虑自定义函数来构建POST数据。提到了一种可能的解决方案是创建一个`postArray()`函数遍历表单元素并构建对象。另一种选择是使用`.serializeArray()`,但这样在接收端需要额外处理数组格式。
摘要由CSDN通过智能技术生成

I am doing an ajax request through post that contains a forms variables.

the data for the ajax request looks like

data : {

somevar1: 'someval1',

somevar2: 'someval2',

somevar3: 'someval3',

somevar4: 'someval4',

form: FORMDATA

}

as you can see, as well as the basic form data I am also passing through some other data.

this means that I cannot use jquery .serialize()

I am wanting to end up with something that I can send through so on the other side I can just do

$_POST['form']['fieldname']

is there a built in function do do this?

or what would I need to do to create one?

the possibility of running a function on the form that does something like

function postArray(form){

var data = {};

var name, value = null;

$(form).children('textarea, input, select'){

name = this.name;

data.name = $(this).val();

}

return data;

}

over the form could work.

and having

data : {

somevar1: 'someval1',

somevar2: 'someval2',

somevar3: 'someval3',

somevar4: 'someval4',

form: postArray(form)

}

would it work?

I could use .serializeArray();

But on the other side I get

Array

(

[0] => Array

(

[name] => name1

[value] => val1

),

[1] => Array

(

[name] => name2

[value] => val2

)

...

[8] => Array

(

[name] => name8

[value] => val8

)

)

Which is close, but that requires me to either

a: loop over the array and convert it to what I want

b: every time i use it loop over it to find the right key

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值