Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax has a problem getting return value from ashx handler

Greetings,

No matter what I do I cannot get my jquery ajax code to get a response other than null from an ashx handler page.

Here is my hmt page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>:: ashx tester ::</title>
    <link rel="stylesheet" href="js/jqueryui/1.8.6/themes/sunny/jquery-ui.css"
        type="text/css" media="all" />
    <script type="text/javascript" src="js/jquery/1.4.3/jquery.min.js"></script>
    <script type="text/javascript" src="js/jqueryui/1.8.6/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/json2/0.0.0/json2.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#btnSubmit').click(
                function () {
                    DoIt();
                }
            );
        });

        function DoIt() {
            $('#btnSubmit').hide();
            $.ajax({
                type: "POST",                
                url: "http://localhost:49424/Handler1.ashx",
                data: {
                    firstName: 'Bob',
                    lastName: 'Mahoney'
                },
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    alert('success: ' + response);
                    $('#btnSubmit').show();
                },
                error: function (response) {
                    alert('error: ' + response);
                    $('#btnSubmit').show();
                }
            });
        }
    </script>
</head>
<body>
    <input id="btnSubmit" type="button" value="Submit" />
</body>
</html>

And here is my ashx page:

Imports System.Web
Imports System.Web.Services
Imports System.Collections.Generic
Imports System.Linq
Imports System.Data
Imports System.Configuration.ConfigurationManager
Imports System.Web.Services.Protocols
Imports System.Web.Script.Serialization

Public Class Handler1
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim j As New System.Web.Script.Serialization.JavaScriptSerializer
        Dim s As String

        s = j.Serialize(Now)
        context.Response.ContentType = "application/json"
        context.Response.Write(s)

    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

Any clues?

Thanks!

Dave

like image 287
Dave Avatar asked Apr 06 '11 05:04

Dave


2 Answers

Try url: "/Handler1.ashx",

instead of

 url: "http://localhost:49424/Handler1.ashx",
like image 53
simplyaarti Avatar answered Nov 09 '22 10:11

simplyaarti


I only use text data type with handlers

      var webServiceURL = 'http://localhost:12400/Handler1.ashx';
        var data = "Key:Value";

        alert(data);

        $("input[id='btnSubmitLead']").click(function () {
            $.ajax({
                type: "POST",
                url: webServiceURL,
                data: data,
                dataType: "text",
                success: function (results) {
                    alert("Your information has been sent to the dealer, thank you");
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus +"-"+ errorThrown);
                    alert("Your information has not been sent");
                }
            });
        });

So, I'm getting information to the handler in all browsers, but I am not getting proper confirmation back, as it's always an error. I'm using an html page to submit the ajax call. I also removed:

         context.Response.ContentType = "application/json"

from my handler which increased the work on the server side, but also the call to the server has been fine... its the return back that has given me issues.

like image 1
Clarence Avatar answered Nov 09 '22 11:11

Clarence