Zag Gol
Zag Gol

Reputation: 1076

AJAX not success returning from asp.net server

as practicing Ajax, I wrote a Ajax call that sends to a local asp.net Handler.ashx a Json with a username and password, and returns true if they are equal to "jhon" "123456", else false. I debugged the code, and I saw that the Handler.ashx receives the call, does the validation, and write to response, but the Ajax call not success.

this the Ajax call:

$.ajax({
    url: '/Handler.ashx',
    dataType: 'json',
    data: {
        name: document.getElementById("username").value,
        password: document.getElementById("password").value
    },

    success: function (json) {
        alert(json.isvalid);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
        alert(textStatus + "  " + errorThrown);
    }
});
alert("failed");

and this is the server side:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        Console.WriteLine("check");
        var name = context.Request["name"];
        var password = context.Request["password"];

        string response = IsValid(name, password) ? "true" : "false";
        context.Response.ContentType = "appliaction/text";
        context.Response.Write("{isvalid:'" + response + "'}");
    }

    private bool IsValid(string name, string password)
    {
        Console.WriteLine("isvalid");
        return (name == "jhon" && password == "123456");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

thanks!

Upvotes: 2

Views: 1399

Answers (2)

Mike Gledhill
Mike Gledhill

Reputation: 29161

The simplest change (which still isn't very pretty) would be to change this...

string response = IsValid(name, password) ? "true" : "false";
context.Response.ContentType = "appliaction/text";
context.Response.Write("{isvalid:'" + response + "'}");

...to this...

string response = IsValid(name, password) ? "true" : "false";
context.Response.ContentType = "application/json";
context.Response.Write("{ \"isvalid\" : \"" + response + "\" }");

(Note that you'd misspelt "application"... and you should tell your caller than you're returning json data.)

I saw that the Handler.ashx receives the call, does the validation, and write to response, but the Ajax call not success.

When you say the "AJAX call not success", does an error appear ?

I would recommend running your code in Google Chrome, open the Developer Options (by hitting F12), go into the Network tab, then refresh your webpage.

Watch the Network tab, click on the URL being called, then check the Request being sent, and the Response coming back.

Then also check the Console tab, to see if any JavaScript errors have quietly been thrown.

Upvotes: 4

Enrique Zavaleta
Enrique Zavaleta

Reputation: 2108

try adding this to your ProcessRequest method

JavaScriptSerializer serializer = new JavaScriptSerializer();

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void ProcessRequest(HttpContext context)
    {
        Console.WriteLine("check");
        ....

        return serializer.Serialize("{ isvalid: '" + response + "' }");
    }

And in the client side

.....
success: function (msg) {
    console.log(msg); //this is for you to see the response in the JS console
    var jsonArray = $.parseJSON(msg.d);
    console.log(jsonArray); //and this is the same
    alert(jsonArray.isvalid);
}
.....

Upvotes: 0

Related Questions