简体   繁体   English

通过ajax将jquery数组发送到rails控制器

[英]Send jquery array via ajax into rails controller

i need to send one array into rails controller via jquery ajax 我需要通过jquery ajax将一个数组发送到rails控制器

jquery CODE jquery代码

$(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;
});

But it shows the error 但它显示错误

Error occurred while parsing request parameters. 解析请求参数时发生错误。 Contents: REXML::ParseException (The document "[object Object]" does not have a valid root): 内容:REXML :: ParseException(文档“[object Object]”没有有效的根目录):

To do an ajax POST request via jquery the data has to be a string. 要通过jquery执行ajax POST请求,数据必须是一个字符串。

$.ajax({
    url: "/create",
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify(formData),
    function(data){
        alert(data)
    }
});

Watch out for JSON being undefined in IE6/7. 注意在IE6 / 7中未定义JSON。 If you need to target those browsers, use this: https://github.com/douglascrockford/JSON-js 如果您需要定位这些浏览器,请使用以下命令: https//github.com/douglascrockford/JSON-js

Could you console.log your array? 你可以console.log你的阵列吗? 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. 在FireFox(安装了FireBug插件)或Chrome中按F12打开开发工具并查看控制台。

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: 我认为这不适用于IE,但您可以使用JSON.stringify将JavaScript对象转换为JSON,这样您就可以将您的msg作为JSON发送:

try: 尝试:

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

The error you're having is from your rails app but you have not posted any code where that error happens. 您遇到的错误来自您的rails应用程序,但您没有发布任何错误发生的代码。

I'm not sure what rexml does in rails so can't realy help you with that one. 我不确定rexml在rails中做了什么,所以不能真正帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM