小编典典

jQuery Ajax发布到C#

ajax

我正在尝试在C#上检索JSON对象,这是我的JavasSciprt帖子,但无法在代码隐藏中处理它,谢谢!

$.ajax({
    type: "POST",
    url: "facebook/addfriends.aspx",
    data: { "data": response.data },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        location = '/facebook/login?URL=' + ReturnURL + '&UID=' + response.authResponse.userID + '&TK=' + response.authResponse.accessToken + '';
    }
});

我试图检索数据,如:

Request.Form["data"]
Request["data"]

阅读 272

收藏
2020-07-26

共1个答案

小编典典

这是Encosia.com提供的示例(我添加了一个form参数)。您不需要访问Page.Form-您可以改用方法参数。

代码隐藏

public partial class _Default : Page 
{
  [WebMethod]
  public static string GetDate(string someParameter)
  {
    return DateTime.Now.ToString();
  }
}

Java脚本

$(document).ready(function() {
  // Add the page method call as an onclick handler for the div.
  $("#Result").click(function() {
    $.ajax({
      type: "POST",
      url: "Default.aspx/GetDate",
      data: {someParameter: "some value"},
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Replace the div's content with the page method's return.
        $("#Result").text(msg.d);
      }
    });
  });
});
2020-07-26