Hi, i'm trying to use the ajax dot net plugin to perform ajax calls to
an AJAX enabled WCF service.

No matter what i do i get an error:
'Object reference not set to an instance of an object.'

However from firebug i can see the JSON object that is getting posted:
{"contactmessage":
{"__type":"ContactMessage","name":"Jon","email":"j...@jon.co.uk","message":"gfd"
}}

So it looks like something is being sent correctly but not handled on
the other end. In the examples that come with this plugin there is no
extra handling needed. He just packages the object up in the
javascript and calls it as the correct type at the other end. I don't
know how! After a couple of hours research i came across the
OnDeserializing method and tried to implement that but to no avail.
Same error. I really can't see where i'm going wrong or how to debug
this! Can anyone point me in the right direction?

Here's my javascript:
$(".ContactForm").prepend("<div class=\"ContactMessage\"></div>");
    var formmessage = $(".ContactForm .ContactMessage").css("display",
"none");

    $(".ContactForm .Submit").click(function(evt) {
                evt.preventDefault();

                var contactmessage = new Object();
                contactmessage.__type = "ContactMessage";
                contactmessage.name = $
("#ctl00_ctl00_CPH_Content_CPH_Content_TB_Name").val();
                contactmessage.email = $
("#ctl00_ctl00_CPH_Content_CPH_Content_TB_Email").val();
                contactmessage.message = $
("#ctl00_ctl00_CPH_Content_CPH_Content_TB_Message").val();

                var data = new Object();
                data.contactmessage = contactmessage;

                $.ajaxDotNet(url + "SendMessage", {
                        verb: "POST",
                        data: data,
                    beforeSend: function(r) {
                            formmessage.html("<p>Sending...</p>");

                            return r;
                    },
                        success: function(obj) {
                                obj = obj.d;
                            formmessage.html("<p>Thank you, your message has 
been
sent!</p><p>" + obj + "</p>");
                        },
                    error: function(x, y, z) {
                            formmessage.html("<p>There was an error sending your
message, please try again.</p><p>" + x.responseText + "</p>");
                        });
                    }
                });
        });

And server code:
[OperationContract]
        public string SendMessage(ContactMessage message)
        {
            try
            {
                return message.SendMessage();
            }
            catch (Exception ex)
            {
                return "There was an error sending the message!<br />"
+ ex.Source + " | " + ex.Message;
            }
        }


[DataContract]
public class ContactMessage
{
    public ContactMessage message;
    public string _Name;
    public string _Email;
    public string _Message;

    public ContactMessage()
    {
        _Name = string.Empty;
        _Email = string.Empty;
        _Message = string.Empty;
    }

    public string SendMessage()
    {
        try
        {
            return "Hello, " + _Name + " : " + _Email + " who said: "
+ _Message;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    [OnDeserializing]
    public void OnDeserializing(StreamingContext sc)
    {
        message = new ContactMessage();
    }

    [DataMember(Name = "name")]
    public string Name
    {
        get { return message._Name; }
        set { message._Name = value; }
    }

    [DataMember(Name = "email")]
    public string Email
    {
        get { return message._Email; }
        set { message._Email = value; }
    }

    [DataMember(Name = "message")]
    public string Message
    {
        get { return message._Message; }
        set { message._Message = value; }
    }
}

Reply via email to