Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将点数组(x,y)传递给web服务?_C#_Javascript_Jquery_Web Services - Fatal编程技术网

C# 如何将点数组(x,y)传递给web服务?

C# 如何将点数组(x,y)传递给web服务?,c#,javascript,jquery,web-services,C#,Javascript,Jquery,Web Services,我正在使用Javascript创建一个点(x,y)数组,MyAreas包含MyAreas[0].x和MyAreas[0].y。现在我想把这个数组传递到WebServices中。我该怎么办?您是否使用Ajax调用您的Web服务 假设你是,这很容易,就像这样: $.ajax({ type: "POST", url: myUrl, data: MyAreas, success: function(result){

我正在使用Javascript创建一个
点(x,y)
数组,
MyAreas
包含
MyAreas[0].x
MyAreas[0].y
。现在我想把这个数组传递到WebServices中。我该怎么办?

您是否使用Ajax调用您的Web服务

假设你是,这很容易,就像这样:

$.ajax({
        type: "POST",
        url: myUrl,
        data: MyAreas,
        success: function(result){
            // success function here
        }
 });
试着这样做:

var points=[point1, point2,..., pointn];
var postData="";
points.each(function(i,p){
    postData+="&points["+i+"].x="+p.x;
    postData+="&points["+i+"].y="+p.y;
});
// postData="points[0].x=12&points[0].y=2&points[1].x=2.....";

$.ajax({
  url:"web service url...",
  contentType:"application/x-www-form-urlencoded; charset=UTF-8",
  type:"post",
  data:postData
  success:function(){
    //todo
  }
});
如果使用asp.net MVC,默认的模型绑定器将解析这样一个postData字符串

Public ActionResult SavePoints(List<Point> points)
{
    ...
}
Public ActionResult保存点(列表点)
{
...
}

如果使用AJAX发布项目:

$.post("myUrl", {points: MyAreas}, function() {
    // callback
});
在C#中:


点数数组是用javascript写的吗?@Leo Cai:是的,它是用javascript写的。var MyAreas=[];并添加值,如-MyAreas.push({x:mouseX,y:mouseY});嗯,您可以
JSON.stringify(MyAreas)
来获取表示您的数据的JSON字符串,这实际上取决于Web服务的期望值
public void SavePoints(Points[] points) {
    // your implementation
}

public class Point {
    public int x {get;set;}
    public int y {get;set;}
}