XmlHttpRequest在webservice中给出null值

时间:2016-11-29 11:01:13

标签: javascript json web-services xmlhttprequest

当我尝试将参数传递给我的webservice时,它会给我一个空值。

Javascript:

    <script>

    var Model = {

    };

    Model.Message = function(item) {
        if (item) {
            this.From = item.From;
            this.Recipients = item.Recipients;
            this.Subject = item.Subject;
            this.Html = item.Html;
        } else {
            this.From = null;
            this.Recipients = null;
            this.Subject = null;
            this.Html = null;
        }
    };

    Model.Recipient = function(item) {
        if (item) {
            this.Email = item.Email;
            this.Name = item.Name;
            this.Type = item.Type;
        } else {
            this.Email = null;
            this.Name = null;
            this.Type = null;
        }
    };

    jQuery(document).ready(function() {
        $("#btnSendEmail").click(function () {

            var message = new Model.Message();
            message.From = document.getElementById("txtEmail").value;
            message.Subject = "Contact Formulier: " + document.getElementById("txtSubject").value;
            message.Html = "Telefoon: " + document.getElementById("txtTelephone").value + "<br/>" +
                "Bedrijf: " + document.getElementById("txtCompany").value + "<br/><br/>" +
                "Bericht: " + "<br/>" +
                document.getElementById("txtMessage").value;
            message.Recipients = [];
            var recipient = new Model.Recipient();
            recipient.Email = "p.borgmans@mobileaccess.be";
            recipient.Name = "Pieter Borgmans";
            recipient.Type = "To";
            message.Recipients.push(recipient);

            var params = JSON.stringify(message);


            var url = "http://localhost:26294/JsonService.svc/SendMail";

            var xhr = new XMLHttpRequest();
            xhr.open("POST", url, true);
            xhr.contentType = "application/json; charset=utf-8";
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.onload = function() {
                if (this.status === 200) {
                    var response = (this.responseText) ? JSON.parse(this.responseText) : this.responseText;
                    $("#lblSendMailMessage").text = response;
                } else {
                    $("#lblSendMailMessage").text = xhr.status + ": " + xhr.statusText;
                }
            };
            xhr.onerror = function (onErrorArgs) {
                $("#lblSendMailMessage").text = "Error";
            };
            xhr.send(params);
        });
    });
</script>

我的网络服务功能:

public void SendMail(Message message)
    {
        if (message != null)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(message.From.Trim());
            mail.Subject = message.Subject;
            mail.Body = message.Html;
            mail.IsBodyHtml = true;
            foreach (var recipient in message.Recipients)
            {
                switch (recipient.Type)
                {
                    case "To":
                        mail.To.Add(new MailAddress(recipient.Email, recipient.Name));
                        break;
                    case "Cc":
                        mail.CC.Add(new MailAddress(recipient.Email, recipient.Name));
                        break;
                    case "Bcc":
                        mail.Bcc.Add(new MailAddress(recipient.Email, recipient.Name));
                        break;
                }
            }
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.Host = "relay.skynet.be";
            client.Send(mail);
        }
    }

我的留言对象:

[Serializable]
[DataContract]
public class Message
{
    [DataMember]
    public string From { get; set; }
    [DataMember]
    public string Subject { get; set; }
    [DataMember]
    public string Html { get; set; }
    [DataMember]
    public List<Recipient> Recipients { get; set; }
}
[Serializable]
[DataContract]
public class Recipient
{
    [DataMember]
    public string Email { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Type { get; set; }
}

有人知道我对这个Web服务的调用可能会出错吗?

提前感谢您的帮助。

0 个答案:

没有答案