2013-05-08 80 views
0

我需要通过jQuery AJAX一个数组送入轨道控制器发送jQuery的数组轨控制器

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

但它显示了在解析发生

错误的错误请求参数。 内容: REXML :: ParseException的(文件“的翻译:”没有一个有效的根):

回答

0

你能CONSOLE.LOG您的阵列?这会很容易,你点击它时可以检查子元素。在FireFox中(安装FireBug插件)或在Chrome中按F12打开开发工具并查看控制台。

我不认为这会为IE浏览器,但你可以将JavaScript对象转换为JSON与JSON.stringify这样你就可以把你的味精为JSON:

尝试:

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

的你遇到的错误是来自你的Rails应用程序,但你没有发布任何错误发生的代码。

我不确定在rails中rexml是什么,所以不能真正帮助你。

2

要通过jQuery做一个ajax POST请求数据必须是一个字符串。

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

注意JSON在IE6/7中未定义。如果您需要定位这些浏览器,请使用:https://github.com/douglascrockford/JSON-js