使用jQuery 1.4在Ajax调用中传递数组 [英] Passing arrays in ajax call using jQuery 1.4

查看:92
本文介绍了使用jQuery 1.4在Ajax调用中传递数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码使用jQuery 1.2.6对我有用,但在1.4中导致代理错误.

The following code works for me using jQuery 1.2.6 but causes a broker error in 1.4.

var items = new Array();
items[0] = "Item 1";
items[1] = "Item 2";
items[2] = "Item 3";

var dataToSend = {'_service' : myService, '_program' : myProgram, 'selections' : items} ;

$.ajax({    
 type: "post",
 url: myURL,
 dataType: "text",
 data: dataToSend,
 success: function(request) {$('#results').html(request); } // End success
}); // End ajax method

我得到的经纪人错误表明选择中传递的是'selections []'

The broker error I get indicates that what is being passed in selections is 'selections[]'

错误:(字段名称"selections []"中的无效字符"[".字段名称中不允许使用此字符.)

jQuery处理ajax调用中的数组的方式是否发生了变化?还是这是传递数组的不正确方法?

Was there a change in how jQuery handles arrays in an ajax call? or was this an incorrect way to pass an array?

任何帮助将不胜感激.

@jvenema的回答解决了我的问题.使用传统"设置,您可以使jQuery处理类似于先前版本的参数.这是一些其他有关更改的链接, jQuery.ajax() jQuery.param()和博客文章

The answer from @jvenema solved my problem. With the "traditional" setting you can cause jQuery to handle the parameters like the previous version. Here are some additional links which talk about the change jQuery.ajax(), jQuery.param() and a blog post jQuery 1.4 $.param demystified.

关于

jQuery.ajaxSettings.traditional = true;

或作为ajax调用中的附加选项

or as an additional option in the ajax call

$.ajax({    
 traditional: true,
 type: "post",
 url: myURL,
 dataType: "text",
 data: dataToSend,
 success: function(request) {
   $('#results').html(request);
 }  // End success
}); // End ajax method

推荐答案

jQuery 1.4已更新为使用PHP语法发送数组.您可以使用以下方法将其切换为旧样式:

jQuery 1.4 was updated to use the PHP syntax for sending arrays. You can switch it into the old style by using:

jQuery.ajaxSetting.traditional = true;

有关详细信息,请参见此处.

See here for details.

这篇关于使用jQuery 1.4在Ajax调用中传递数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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