【问题标题】:How to pass Array of Points(x,y) to web services?如何将点数组(x,y)传递给 Web 服务?
【发布时间】:2013-01-09 10:27:31
【问题描述】:

我正在使用 Javascript 创建一个 Point(x,y) 数组,MyAreas 包含 MyAreas[0].xMyAreas[0].y。现在我想将此数组传递给 WebServices。我该怎么办?

【问题讨论】:

  • 是javascript中的点数组吗?
  • @Leo Cai:是的,它在 Javascript 中。 var MyAreas = [];并添加值 - MyAreas.push({ x: mouseX, y: mouseY });
  • 好吧,你可以JSON.stringify(MyAreas) 得到一个代表你的数据的 JSON 字符串——这真的取决于 web 服务的期望

标签: c# javascript jquery web-services


【解决方案1】:

如果您使用 AJAX 发布您的项目:

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

在 C# 中:

public void SavePoints(Points[] points) {
    // your implementation
}

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

【讨论】:

    【解决方案2】:

    您是否使用 Ajax 来调用您的网络服务?

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

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

    【讨论】:

      【解决方案3】:

      试试看。像这样:

      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,默认的model binder cab会解析这样一个postData字符串。

      Public ActionResult SavePoints(List<Point> points)
      {
          ...
      }
      

      【讨论】:

        猜你喜欢
        • 2012-04-12
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 2014-12-17
        • 1970-01-01
        • 2019-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多