Getting Null response instead of json in jquery post from aspx.cs page

I am working on simple application where i am hitting the Request.aspx page from default2.aspx using jquery ajax post method as you can see below:

js page:

$(document).ready(function () {
$("#login").click(function () {
    var email = $("#email").val();
    var password = $("#password").val();
    $.ajax({
        type: "POST",
        url: "Request.aspx?Login=True",
        data: "{'username': '" + email + "','password': '" + password + "'}",
        cache: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            console.log(response);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var url = "http://google.com";
            //$(location).attr('href', url);
        }
    });
});

});

Default.aspx:

<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

<script src="Scripts/LoginCode.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <div class="container">
        <div class="main" id="loginform">
            <label>
                Email :</label>
            <input type="text" name="demail" id="email" />
            <label>
                Password :</label>
            <input type="password" name="password" id="password" />
            <input type="button" name="login" id="login" value="Login" />
        </div>
    </div>
</div>
</form>
</body>

Request.cs Page:

 protected void Page_Load(object sender, EventArgs e)
{

    if (Request.QueryString["Login"] != null)
    {

            test();

    };

}



private string test()
{
    var response = new ResponseResult();
    var javaScriptSerializer = new JavaScriptSerializer();
    response.Status = -1;
    response.ErrorMessage = "";
    response.Action = "UserProjects";
    response.Data = "The Username or password you entered is not valid";
   return javaScriptSerializer.Serialize(response);
}

public class ResponseResult
{
    public string Action { get; set; }
    public int Status { get; set; }
    public string ErrorMessage { get; set; }
    public object Data { get; set; }
}

Problem: In response NUll is coming but i am expected json. Please let me know where i am going wrong.

If you need more info i will try my best to provide.

Thanks in advance.

Answers 1

  • It is a good idea to use Restful Webservices or Web Method to call from AJAX.

    But still you can do the following.

    Use Response.Write() instead of returning string.

    Use this code.

    private void test()
    
    {
        var response = new ResponseResult();
        var javaScriptSerializer = new JavaScriptSerializer();
        response.Status = -1;
        response.ErrorMessage = "";
        response.Action = "UserProjects";
        response.Data = "The Username or password you entered is not valid";
        string result = javaScriptSerializer.Serialize(response);
    
        Response.Clear();
        Response.ContentType = "application/json; charset=utf-8";
        Response.Write(result);  //write json string to output   
    }
    

Related Articles