通过ajax将jquery数组发送到rails控制器 [英] Send jquery array via ajax into rails controller

查看:88
本文介绍了通过ajax将jquery数组发送到rails控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过jquery ajax将一个数组发送到rails控制器

i need to send one array into rails controller via jquery ajax

jquery CODE

jquery CODE

$(document).ready(function(){

var counter = 2;
$("#addButton").click(function () {
   var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'TextBoxDiv' + counter);

   newTextBoxDiv.after().html('<input type="text" placeholder="Role" name="Role' + 
   counter + 
      '" id="textbox' + counter + '" value="" > <input type="text" 
placeholder="Search" name="search' + counter + 
      '" id="se" value="" >');

newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
});



$("#getButton").click(function () {
var fd = new FormData($("#movie-form")[0]); 
var name = document.getElementById("file").files[0].name;
var arr = [];
var msg = '';
for(i=1; i<counter;i++){
  msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val(); 
      arr[i] = $('#textbox' + i).val();
}
 $.each(arr,function(index, item)
{   
     alert(index);
     alert(item);
   }
);

fd.append( 'file', name);
fd.append( 'file22', name);
$.ajax({
url: '/create',
data: {fd:fd,arr:arr},
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
return false;
});

但它显示错误

解析请求参数时发生错误。
内容:
REXML :: ParseException(文档[object Object]没有有效的根目录):

推荐答案

你可以console.log你的阵列吗?这将更加轻松,您可以在单击它时检查子元素。在FireFox(安装了FireBug插件)或Chrome中按F12打开开发工具并查看控制台。

Could you console.log your array? That'll be much easer and you can inspect the sub elements when you click on it. In FireFox (with FireBug plugin installed) or in Chrome press F12 to open the dev tools and see the console.

我认为这不适用于IE但你可以使用JSON.stringify将JavaScript对象转换为JSON,这样您就可以将您的消息作为JSON发送:

I don't think this will work for IE but you can convert a JavaScript object to JSON with JSON.stringify so you can send your msg as JSON:

尝试:

for(i=1; i<counter;i++){
      arr[i] = $('#textbox' + i).val();
}
msg= JSON.stringify(arr)

你的错误'来自你的rails应用程序,但你没有发布任何错误发生的代码。

The error you're having is from your rails app but you have not posted any code where that error happens.

我不确定rexml在rails中做了什么,所以无法真正帮助你有那个。

I'm not sure what rexml does in rails so can't realy help you with that one.

这篇关于通过ajax将jquery数组发送到rails控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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