1

I'm setting up a simple POST method to send information to a form before processing the C# in the code-behind. The expected result is a plain-text string with some simple values:

function postTasks() {
  var postdata = $("#taskReturnDiv").text();
  try {
    $.ajax({
        type: "POST",
        url: "calendar.aspx",
        cache: false,
        data: postdata,
        dataType: "text",
        error: getFail
    });
  }
  catch (e) {
    alert(e);
  };

  function getFail(data, textStatus, jqXHR) {
    alert(textStatus);
  };
};

However, attempting to read with the following:

string processTaskPostback(HttpContext context)
{
    string taskString = String.Empty;
    HttpContext.Current.Request.InputStream.Position = 0;
    using (StreamReader inputStream = new StreamReader(HttpContext.Current.Request.InputStream))
    {
        taskString = inputStream.ReadToEnd();
        return taskString;
    }
}

Results in taskString containing a value that seems to be the reference to the statebag rather than the expected string:

__VIEWSTATE=RGl0btFvBz93yS%2BQp%2FHpk1pT9AohsFsyJI90RjT3BvtWkw3DPYDjGhqIGHSADWaCoXsRXxWSmdlwsbebI7qMhxn%2FEKZiDTH9RB6TB97HFurOenlTG3sXGe6r2a2MqaTCIkYUZVbLp8FQuyPQmG%2FdKCXeUrjUIYUGBoD%2FvB8xF3ThhppKd3OAsPydvVQkB4z4CkygDtcZwP6IckX52YX%2BE3ttAEOOUVfjjMY5lXaiB56EwldbcRvJP6nIKz1SeQodGNgeYSOFnMO1zht0ouMRBbUYb5K3fAuB5zHFogpmyfd4K9whgnGKwcyf1dXzwlli&newEvent_title=1&newEvent_date=2018-03-21&newEvent_description=1&addNewPersonalEvent=Add+Event&__VIEWSTATEGENERATOR=B66867E1&__EVENTVALIDATION=9z3SFY4WzFb%2BAXZpcZVK5W7ZwbkYcJ3I43tG39FSX4H7PRykGGlQ4TS7%2F%2Bfs34wWJXo1WSdDRheOljoJFm8Cc6B0Q%2Bwl3LbkKGAKt1ifl%2F6B5XBxW9eUwE%2BeYa0dlJIiY08t05OKyGu%2FF03cZOgZnbSNYMlTcajFwaWwnU5PHKLsXd%2FNVWyxfvoEy%2BAFmFRc

What am I missing here?

  • asp.net used to include viewstate by default, you can turn it off:https://stackoverflow.com/a/647456/264607. If you're doing a post from a page that included viewstate then it will send it. It's normally put in as a hidden field within form, do you have a form within taskReturnDiv? – BlackICE Mar 20 '18 at 18:29
  • @BlackICE No, the opposite. `taskReturnDiv` is inside a form. –  Mar 20 '18 at 18:47
  • @BlackICE Will turning off viewstate impact my use of session data in the realm of `HttpContext.Current`? –  Mar 20 '18 at 18:48
  • @BlackICE Incidentally, turning off Viewstate has had no effect. –  Mar 20 '18 at 18:51
  • try view source and see if you have those hidden inputs within taskReturnDiv – BlackICE Mar 20 '18 at 19:26
  • Why are you using StreamReader? – ElasticCode Mar 20 '18 at 19:33
  • How are you calling `processTaskPostback`? – wazz Mar 20 '18 at 20:06
  • `The expected result is a plain-text string with some simple values:` Please update your post with the contents of the plain-text string. – mjwills Mar 20 '18 at 20:42
  • if you are trying to use a static WebMethod to serve ajax() call, use following decorator and the method must be static and inside your calendar.aspx.cs class //Code [WebMethod()] public static person GetData(string name) – Antonio Avndaño Duran Mar 20 '18 at 20:45

1 Answers1

0
//static WebMethod to serve ajax() call

    [WebMethod()]

    public static person GetData(string name)

    {

        person p= new person();

        p.name = name;

        return p;

    }

}

// Call must be done in this way

jQuery.ajax({

        url: '[ASPX file Name Here].aspx/GetData',

        type: "POST",

        dataType: "json",

        data: "{'name': '" + name + "'}",

        contentType: "application/json; charset=utf-8",

        success: function (data) {

            alert(JSON.stringify(data));

        }

    });