将数组从 javascript 传递给 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8871586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass an array from javascript to c#
提问by Ovi
I have a array in javascript and i need to get it to my c# webMethod. what is the best way to do this?
我在 javascript 中有一个数组,我需要将它放到我的 c# webMethod 中。做这个的最好方式是什么?
my c# code is like this:
我的 C# 代码是这样的:
[WebMethod]
public static void SaveView(string[] myArray, string[] filter)
{
}
EDIT--
编辑 -
My json data looks like this:
我的 json 数据如下所示:
{"myArray":[{"name":"Title","index":"Title","hidden":false,"id":"1","sortable":true,"searchoptions":{"sopt":["cn","eq","bw","ew"]},"width":419,"title":true,"widthOrg":150,"resizable":true,"label":"Title","search":true,"stype":"text"},{"name":"Author","index":"Author","hidden":false,"id":"3","sortable":true,"searchoptions":{"sopt":["cn","eq","bw","ew"]},"width":419,"title":true,"widthOrg":150,"resizable":true,"label":"Author","search":true,"stype":"text"}]}
But i doesnt work... any idea why?
但我不工作......知道为什么吗?
Thank you very much.
非常感谢。
采纳答案by Darin Dimitrov
You could send it as a JSON string. Here's an example using jQuery:
您可以将其作为 JSON 字符串发送。下面是一个使用 jQuery 的例子:
var array = [ 'foo', 'bar', 'baz' ];
$.ajax({
url: '/foo.aspx/SaveView',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ myArray: array }),
success: function(result) {
}
});
If your Page Method returns something, you should use the result.dproperty in the success callback to fetch the result of the page method call.
如果你的页面方法返回了一些东西,你应该使用result.d成功回调中的属性来获取页面方法调用的结果。
If you don't use jQuery, you will have to manually account for browser differences in sending the AJAX request. But for this to work there are 2 crucial things to be included in the request:
如果您不使用 jQuery,则必须手动考虑浏览器在发送 AJAX 请求时的差异。但是要使其正常工作,请求中需要包含两件重要的事情:
- The Content-Type request header must be set to
application/json - The request payload should be JSON, for example:
{ myArray: [ 'foo', 'bar', 'baz' ] }
- Content-Type 请求头必须设置为
application/json - 请求负载应该是 JSON,例如:
{ myArray: [ 'foo', 'bar', 'baz' ] }
UPDATE:
更新:
Now that you have updated your question it seems that you are no longer willing to send an array of strings. So define a model that will match the JSON structure you are sending:
现在您已经更新了您的问题,您似乎不再愿意发送字符串数组。因此,定义一个与您发送的 JSON 结构相匹配的模型:
public class Model
{
public string Name { get; set; }
public string Index { get; set; }
public bool Hidden { get; set; }
public int Id { get; set; }
public bool Sortable { get; set; }
public SearchOption Searchoptions { get; set; }
public int Width { get; set; }
public bool Title { get; set; }
public int WidthOrg { get; set; }
public bool Resizable { get; set; }
public string Label { get; set; }
public bool Search { get; set; }
public string Stype { get; set; }
}
public class SearchOption
{
public string[] Sopt { get; set; }
}
and then:
进而:
[WebMethod]
public static void SaveView(Model[] myArray)
{
}
回答by Raynos
var xhr = new XMLHttpRequest();
xhr.open("POST", "mypage/SaveView");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({ myArray: someArray }));

