There is such a function (hung on button.onclick):

function addNewUser() { var dataToSend = ""; dataToSend += "name=" + $("#name").val(); dataToSend += "&email=" + $("#email").val(); dataToSend += "&login=" + $("#username").val(); dataToSend += "&pass=" + $("#password").val(); alert(dataToSend); $.ajax({ type: "POST", url: "registration.ashx", // данные, которые будут отправлятся на сервер с запросом data: dataToSend, success: function(data) { alert(data); } }); }; 

dataToSend is filled correctly. The request itself also occurs, but dataToSend does not reach registration.ashx.

Code registration.ashx:

 public class registration : IHttpHandler { public void ProcessRequest(HttpContext context) { Thread.Sleep(2000); var parametrs = context.Request.QueryString; context.Response.ContentType = "text/plain"; // проверка параметров if (parametrs["login"] == "user" && parametrs["pass"] == "12345") { // отправка ответа context.Response.Write("Авторизация прошла успешно"); } else { context.Response.Write("Ошибка авторизации"); } } public bool IsReusable { get { return false; } } 

As a result, an authorization error is always returned. Already broke his head - what's the matter?

  • what does the console write? the data go away? array / object did not try to send? masochism so form a string)) - Jean-Claude
  • Passing additional parameters with an AJAX seems to be data: {name:value}, where name is the variable name and value is the variable value - Dmitriy Kondratiuk
  • @DmitriyKondratiuk look to the docks, string / object / array. - Jean-Claude

2 answers 2

The data in the POST request is not transferred to the QueryString , but in the body of the request.

Make on the client:

 url: "registration.ashx?" + dataToSend, 

or on the server:

 var parametrs = context.Request; 

    You can try to arrange data as an object and return json from the server:

    javascript:

     function addNewUser() { var data = { name: $("#name").val(), email: $("#email").val(), login: $("#username").val(), pass: $("#password").val() }; alert(data); $.ajax({ type: "POST", url: "registration.ashx", dataType: 'json' // ожидаем с сервера json data: data, success: function(response) { if (response && response.result === 'success') {} else {} alert(response); } }); }; 

    c #:

     /* ... */ context.Response.ContentType = "application/json"; /* ... */ context.Response.Write("{ \"result\": \"success\", \"msg\": \"Авторизация прошла успешно\" }");