javascript中的数组到服务器端代码 [英] array in javascript to server side code

查看:74
本文介绍了javascript中的数组到服务器端代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这个代码工作,它会打印从gmail请求获得的所有电子邮件地址

但现在我需要将带有结果的数组发送到另一个局部视图

如何以可以在其他地方使用它的方式存储它? (部分视图确实出现在同一页面上)



  var 数组= response.contacts.asArray(); 
var html = 你有 + array.length + contacts< BR />;
html + = < table cellpadding = 20>;
for var i = 0 ; i < array.length; i ++){
html + = < span class =code-string>< tr>< td align = center valign ='bottom'>;
html + = array [i] .email + < / td>< / tr> ;
}
html + = < / table>;
document.getElementById(' contacts')。innerHTML = html;
} else {
alert(' 错误:' + response.errorMessage);
}





非常感谢!

解决方案

你可以使用JSON来表示您的数组或任何其他对象。将您拥有的数组转换为JSON字符串,然后将字符串发送到您想要的任何地方,并在接收端从JSON字符串重新创建数组。



代码将看起来像这样

  //  发件人 
var data = JSON .stringify(MyObject);
发送(数据);

// receiver
MyObject = JSON .parse(data);



http: //www.json.org/ [ ^ ]


 var mailList = {mailList:mailListArray}; 


.ajax({
type:POST,
url:''@ Url.Action(MailListArray,mail)'',
data:mailList,
success:function(data){
alert(data.Result) );
},
dataType:json,
传统:true
});



这是一个打电话给一个mvc控制器。

传统的:真的



这是成功的关键!

感谢;

http://stackoverflow.com/questions/309115/how-can-i-post-an-array-of-string-to-asp -net-mvc-controller-without-a-form [ ^ ]



谢谢你们所有


Hi
I have this code working, it prints all email adresses it gets from a request to gmail
but now I need to send the array with results to another partial view
how can I store it in a way that I can use it somewhere else? (the partialview does appear on the same page)

var array = response.contacts.asArray();
               var html = "You have " + array.length + "contacts<BR/>";
               html += "<table cellpadding=20>";
               for (var i = 0; i < array.length ; i++) {
                   html += "<tr><td align=center valign='bottom'>";
                   html += array[i].email + "</td></tr>";
               }
               html += "</table>";
               document.getElementById('contacts').innerHTML = html;
           } else {
               alert('Error :' + response.errorMessage);
           }



thanks a lot!

解决方案

you can use JSON to represent your array, or any other object for that matter. Convert the array you have to a JSON string, then send the string to anywhere you want and on the receiving end recreate the array from the JSON string.

The code will look something like this

//sender
var data= JSON.stringify(MyObject);
Send(data);

//receiver
MyObject = JSON.parse(data);


http://www.json.org/[^]


var mailList = { mailList: mailListArray };


.ajax({ type: "POST", url: ''@Url.Action("MailListArray", "mail")'', data: mailList, success: function (data) { alert(data.Result); }, dataType: "json", traditional: true });


this is a call to a mvc controller.

traditional: true


this was the key to succes!
thanks to;
http://stackoverflow.com/questions/309115/how-can-i-post-an-array-of-string-to-asp-net-mvc-controller-without-a-form[^]

thanks y''all


这篇关于javascript中的数组到服务器端代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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