2

Trying to use this code

i reached a dead end, in the client side the form values collcected and serialized , but since it's not possible to use an instance of request in a static method i failed to receive the seialized form values at the server, i tried to bypass it by using the static HttpContext.Current.Request.InputStream but i got empty stream. how can i get the input stream in the server ? client side :

 function myFunction() {
             $.ajax({
             type: "POST",
             url: "ajaxForm.aspx/Receiver",
             contentType: "application/json; charset=utf-8",
             data: $('#myForm').serialize(),
             datatype : "json",
             cache: false,
             success: function (data) {
                 $('#result').html(data);
             },
             error: function (data) {
                 alert('failed');
             }
         });
     }

server side first version (copied from that link):

{
string json;
using(var reader = new StreamReader(Request.InputStream)){
    json = reader.ReadToEnd();
}

second version :

  [WebMethod ]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static void  Receiver()
{
    if (HttpContext.Current.Request.InputStream.Position != 0)
    {
                 HttpContext.Current.Request.InputStream.Seek(0,System.IO.SeekOrigin.Begin);
    }
    byte[] data = new byte[HttpContext.Current.Request.InputStream.Length];
    HttpContext.Current.Request.InputStream.Read(data, 0, data.Length);}
Levil
  • 41
  • 5
  • Try to use the answer from this question http://stackoverflow.com/questions/4758575/how-can-i-access-session-in-a-webmethod – SergeyAn Jan 21 '16 at 07:49

2 Answers2

0

Currently your data doesn't look like JSON. Try like this.

var jsonData = JSON.stringify({
    form: $('#myForm').serialize()
});

in the ajax call for data

...
contentType: "application/json; charset=utf-8",
             data: jsonData,
             datatype : "json",
...

Your method:

[WebMethod ]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static void  Receiver(string form)
{
     //in the string form you should have myForm serialize.
}

If you want to use Context.Session for something, you need to enable it.

[System.Web.Services.WebMethod(EnableSession = true)]
mybirthname
  • 17,949
  • 3
  • 31
  • 55
0

This is an old post, but I recently ran into a similar issue with a client and figured I'd share my experience.

The client had a legacy app that was running jQuery 1.7.1 (were afraid to change this) and had recently set up a page to accept POST only. In jQuery 1.7.1 (and possibly others), I learned you have to pass the type (as well as the method?) for POSTs.

So, this

$.ajax({
    url: {url...},
    data: {data...},
    method: 'POST',
    ...
});

became, this

$.ajax({
    url: {url...},
    data: {data...},
    method: 'POST',
    type: 'POST',
    ...
});

And everyone lived happily ever after.

Hopefully, this saves someone else some headache.

critical_error
  • 6,306
  • 3
  • 14
  • 16