-4

I tried this page method code in my ASPX page (ShowUsers.aspx), but the function gave me a 500 server error and does not go in to the success handler.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetInfo(string email)
{
    JavaScriptSerializer jss = new JavaScriptSerializer();
    User user = UserService.GetUser(email);
    return jss.Serialize(user);
}

And the JavaScript:

$(document).ready(function() {
  $("#l").click(function() {
    $("#modalUpdate").fadeIn(1000);
    var url = "ShowUsers.aspx/GetInfo";
    var email = "davidizhakinew@gmail.com";

    $.ajax({
      url: url,
      type: "POST",
      data: "{ email : " + email + " }",
      dataType: "JSON",
      contentType: "application/JSON; charset=utf-8",
      success: function(msg) {
        alert("success");
        var data = JSON.parse(msg.d);
        // do stuff (trimmed)
      },
      error: function(msg, xhr) {
        alert(msg + ", " + xhr.responseText);
      }
    });
  });
});

Can someone help me please?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
David Izhaki
  • 23
  • 1
  • 6
  • F12->Network -> Response will show you the exception. It will be a null exception since you are reading querystring parameters (string email) but not providing them in the Url. To bind Post data to function parameters, but [FromForm] before string email. – Henrik Erlandsson Jun 14 '22 at 14:42

3 Answers3

1

AJAX call is returning status code 500. This is definitely an error on C# code, not JavaScript.
Status code 500 means Internal Server Error. ASP.NET applications throw that code when any exception occurs. Take a look in your logs / console output on C# side.

I suppose that the problem is in the method name. If not otherwise specified, due to ASP.NET naming conventions, your GetInfo is callable with GET method. You are calling this endpoint with POST, try to change that to GET.

Also, try other URL combinations, like:

  • ShowUsers/GetInfo
  • ShowUsers.aspx/Info
  • ShowUsers/Info
Mario Cianciolo
  • 1,223
  • 10
  • 17
1

Your JSON is invalid as you have not enclosed the email-address string inside quotes in JavaScript.

Programmatic proof that this will cause a server-side exception:

void Main()
{
    string email = "davidizhakinew@gmail.com";
    var postback = new Postback { email = email };

    Console.WriteLine(new JavaScriptSerializer().Deserialize<Postback>("{ email : '" + email + "' }")); // email string escaped, ok
    Console.WriteLine(new JavaScriptSerializer().Deserialize<Postback>("{ email : " + email + " }")); // YOUR INPUT, exception Invalid JSON primitive
}

public class Postback
{
    public string email { get; set; }
}

A server-side exception manifests itself as an Internal Server Error, code 500. You should be able to see the exception details in the Windows event viewer.

So in JavaScript just wrap the email address inside quotes:

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

Also note that all you need for your web method body is

public static User GetInfo(string email) {
   return UserService.GetUser(email);
}

i.e. you do NOT need to spin up a serializer yourself. The Framework selects and spins up the necessary serializer (JSON or XML) based on the request headers.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
0

Create a new page and receive data from that page instead of receiving from the same page in asp.net It is wrong to call

`"ShowUsers.aspx/GetInfo";`

try this with new page

`showUsersAjax.aspx'`

and make sure that your url should be like

var url = "http://www.doamin.com/ShowUsers.aspx/GetInfo";

try this

var url = "http://www.doamin.com/ShowUsers/GetInfo";

Assuming that ShowUsers is controller and GetInfo is the action additionally, add POST signature in your API.

Abdul Qayyum
  • 1,024
  • 7
  • 15