在webmethod中检索已发布的json数据

时间:2015-08-28 21:52:48

标签: c# asp.net ajax web-services

我正在尝试使用ajax上传一些json数据并在服务器端检索。我确实获得了成功'上传时提醒但无法打印发布的数据。我在这做错了什么?

var data = {
    "UserID" : 1,
    "Name" : "Sara"       
}

$.ajax({
   type: 'POST',
   url: "../somepage.aspx/ProcessRequest", 
   data: JSON.stringify(data),                              
   success: function (msg) {
      alert("Success");
   },
   error: function (err) {
      console.log(err);
   }
});

服务器端:

[ScriptService]
public partial class uploadInspection : System.Web.UI.Page
{
[WebMethod]
public void ProcessRequest()
{
   var jsonString = String.Empty;
   var resp = String.Empty;

   HttpContext.Current.Request.InputStream.Position = 0;
   using (var inputStream = new StreamReader(this.Request.InputStream))
   {
      jsonString = inputStream.ReadToEnd();
   }
   var Items = JsonConvert.DeserializeObject<Items>(jsonString);              


   this.Response.ContentType = "application/json";
   this.Response.ContentEncoding = Encoding.UTF8;
   this.Response.Write(Items);

}

public class Items
{
    public int UserID { get; set; }
    public string Name { get; set; }
}

0 个答案:

没有答案