Link to home
Start Free TrialLog in
Avatar of mgmhicks
mgmhicks

asked on

How to get $ajax to come back succesful

I have the following jquery that sends the data to a ashx file just as expected.  So I get the information send it back with the ashx file but it keeps running the error code rather than the successful code.  I',m not receiving error in ashx file, so not sure if I have to send a successful back or not.  the 2nd set of code Is the ashx file.  So can someone tell me what determines a fail or not.

thanks


 
 $.fn.serializeObject = function () {
                var o = {};
                var a = this.serializeArray();
                $.each(a, function () {
                    if (o[this.name] !== undefined) {
                        if (!o[this.name].push) {
                            o[this.name] = [o[this.name]];
                        }
                        o[this.name].push(this.value || '');
                    } else {
                        o[this.name] = this.value || '';
                    }
                });
                return o;
            };


$.ajax({url: 'AppHandler.ashx',
                type: 'POST',
                data: JSON.stringify($('form').serializeObject()),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    alert(data);
                },
                error: function (errorText) {
                    alert("Wwoops something went wrong !");
                }
            });

Open in new window



Imports System
Imports System.Web
Imports System.IO
Imports System.Web.Script.Serialization
Public Class AppHandler
    Implements System.Web.IHttpHandler

    Public Class App1
        Public Property App1FName As String
            Get
                Return mApp1FName
            End Get
            Set(value As String)
                mApp1FName = value
            End Set
        End Property
        Private mApp1FName As String
        Public Property App1LName As String
            Get
                Return mApp1LName
            End Get
            Set(value As String)
                mApp1LName = value
            End Set
        End Property
        Private mApp1LName As String
    End Class
    Public Class App2
        Public Property App2FName As String
            Get
                Return mApp2FName
            End Get
            Set(value As String)
                mApp2FName = value
            End Set
        End Property
        Private mApp2FName As String
        Public Property App2LName As String
            Get
                Return mApp2LName
            End Get
            Set(value As String)
                mApp2LName = value
            End Set
        End Property
        Private mApp2LName As String
    End Class




    Public Class userInfo
        Public Property first_name() As String
            Get
                Return m_first_name
            End Get
            Set(value As String)
                m_first_name = value
            End Set
        End Property
        Private m_first_name As String
        Public Property last_name() As String
            Get
                Return m_last_name
            End Get
            Set(value As String)
                m_last_name = value
            End Set
        End Property
        Private m_last_name As String
    End Class

    Public Class Address
        Public Property City() As String
            Get
                Return m_City
            End Get
            Set(value As String)
                m_City = value
            End Set
        End Property

        Private m_City As String

        Public Property State() As String
            Get
                Return m_State
            End Get
            Set(value As String)
                m_State = value
            End Set
        End Property
        Private m_State As String
    End Class
    Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "text/plain"
        Try
            Dim strJson As String = New StreamReader(context.Request.InputStream).ReadToEnd()
           
            Dim meJason As New JavaScriptSerializer

            'deserialize the object

            Dim objApp1 As App1 = meJason.Deserialize(Of App1)(strJson)
            Dim objApp2 As App2 = meJason.Deserialize(Of App2)(strJson)
            Dim objAddress As Address = meJason.Deserialize(Of Address)(strJson)
            Dim fullname As String = "test'"
            If objApp1 IsNot Nothing Then

                '    Dim fullName As String = objApp1.first_name + " " + objApp1.last_name
                Dim Address As String = objAddress.City + " " + objAddress.State
                If String.IsNullOrEmpty(fullname) Then
                    context.Response.Write(String.Format("Name :{0}", fullname))
                Else
                    Dim myName As String = objApp1.App1FName + Space(1) + objApp1.App1LName

                    context.Response.Write(String.Format("Name :{0}, Address:{1}", myName, "test"))
                End If

            Else
                context.Response.Write("No Data")
            End If
        Catch ex As Exception
            context.Response.Write("Error :" + ex.Message)
        End Try
    End Sub

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



End Class

Open in new window

Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

Are you getting the alert from your ajax error function?  If so, that means that the ajax call itself is failing.  It has nothing to do with what's returned from AppHandler.ashx.  To clarify, what I mean is that if you have an error clause in AppHandler.ashx, the ajax call will return that to the success function.  The ajax error function is hit when there is something wrong with the call itself --- such as the data being returned not matching what's defined as the return data type in the call.
More specifically, I don't see where you're json encoding your returns from AppHandler.ashx
Avatar of mgmhicks
mgmhicks

ASKER

this line is what returns the data.

context.Response.Write(String.Format("Name :{0}, Address:{1}", myName, "test"))
Does this have to be encoded then?
I removed the dataType and contentType from the script and it worked fine.

thanks
ASKER CERTIFIED SOLUTION
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you again.