1

我在我的应用程序中使用了ajax,我必须使用该$.post方法。

通常,发送到服务器的数据是键值对。我可以让他们通过:

HttpContext.Current.Request.QueryPara['name'];
....

但在某些情况下,要发送到服务器的数据不包含名称。

它只是一个 xml 段。

像这样:

var data='<data>xxxxx<data>';
$.post('http://server/service.asmx/test',data,function(){
  //callback
},'xml');

那么如何在我的 webmethod 中获取数据?

4

2 回答 2

0

您应该必须使用 'json' dataType 将数据传递给 web 方法。

看看样本:

服务.asmx


[ScriptService]
[WebService(Namespace = "http://localhost/testapp/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService{
   [WebMethod]
   public int Square(int no){ return no * no;}
}

和 JavaScript 代码来请求这个webmethod

<script type="text/javascript">
$(function () {
    $("#button1").click(function () {
        $.ajax({
            type: "post",
            url: "service.asmx/Square",
            data: "{no: 10}",
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                alert("Result :" + data.d);
            },
            error: function (src,type,msg) {
                alert(msg); //open JavaScript console for detailed exception cause
            }
        });
    });
});
</script>

<body>
  <input type="button" id="button1" value="Square" />
</body>
于 2012-08-08T01:39:58.590 回答
0

我不确定 asp.net 是否可以做到这一点。但你总是可以提供这样的名称:

var data= { data: '<data>xxxxx<data>'};
$.post('http://server/service.asmx/test',data,function(){
  //callback
},'xml');

然后在服务器上,您可以通过

var data = Request.Params("data");
于 2012-08-08T03:34:20.590 回答