2013-01-09 39 views
0

我使用Javascript創建了一個Point(x,y)的數組,其中包含MyAreas[0].xMyAreas[0].yMyAreas。現在我想將這個數組傳遞給WebServices。我該怎麼做?如何將點數組(x,y)傳遞給Web服務?

+0

是在點數列中的JavaScript? –

+0

@Leo Cai:是的,它使用Javascript。 var MyAreas = [];和添加值 - MyAreas.push({x:mouseX,y:mouseY}); –

+0

那麼,你可以'JSON.stringify(MyAreas)'得到一個代表你的數據的JSON字符串 - 它確實取決於web服務期望的 – Basic

回答

0

您是否在使用Ajax調用您的webservice?

假設你是,這很容易,就像這樣:

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

嘗試做某事。像這樣:

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,默認的模型綁定器cab將解析postData這樣的字符串。

Public ActionResult SavePoints(List<Point> points) 
{ 
    ... 
} 
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;} 
} 
相關問題