.serialize()Javascript中的变量数组 [英] .serialize() an array of variables in Javascript

查看:81
本文介绍了.serialize()Javascript中的变量数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可用的变量列表,我想通过$ .ajax发布.使用.serialize函数时,我必须保留哪种格式?我不断收到此错误:

I have a list of variables available to me and I want to send it via $.ajax post. What format would I have to keep these in to use the function .serialize? I keep getting this error:

对象空白"没有方法序列化"

Object 'blank' has no method 'serialize'

我试图使它们成为数组,并且尝试了jQuery.param().我觉得这很简单,但似乎无法理解.谢谢!

I've tried to make them an array and I've tried jQuery.param(). I have a feeling this is simple but I can't seem to get it. Thanks!

var $data = jQuery.makeArray(attachmentId = attachmentID, action = 'rename', oldName = filename, newName, bucketName, oldFolderName, newFolderName, projectId = PID, businessId = BID);
var serializedData = $data.serializeArray();
//alert(theurl);

$.ajax({ type: "post", url: theurl, data: serializedData, dataType: 'json', success:  reCreateTree });  

推荐答案

.serialize适用于表单元素:

.serialize is for form elements:

将一组表单元素编码为要提交的字符串.

Encode a set of form elements as a string for submission.

$.ajax文档表示data选项:

要发送到服务器的数据.如果还不是字符串,则将其转换为查询字符串.它被附加到GET请求的URL上.请参阅processData选项以防止此自动处理.对象必须是键/值对.如果value是一个Array,则jQuery会根据传统设置(如下所述)的值,使用相同的键序列化多个值.

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

因此,您所需要做的就是传递一个对象.例如:

So all you need to do is passing an object. For example:

$.ajax({ 
    type: "post", 
    url: theurl, 
    data: {                             // <-- just pass an object
          attachmentId: attachmentID,
          action: 'rename',
          // ...
    },
    dataType: 'json', 
    success:  reCreateTree 
});  

这篇关于.serialize()Javascript中的变量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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