簡體   English   中英

從HTML頁面調用Web服務

[英]Calling web service from HTML page

我有一個Web服務,我想從HTML頁面調用它。 我在HTML頁面中使用了以下代碼:

function SendMail() {
         var requestdata = {};
     //    requestdata.Name = "Amar Prakash";
       //  var pdata = { "objRequestData": requestdata };

         $.ajax({
             type: "POST",
             data: JSON.stringify(pdata),
             url: "http://localhost:2345/WebService.asmx/Send_Mail",
             contentType: "application/json;charset=utf-8",
             dataType: "json",
             success: function (data) {
                 alert("SSSSSSSSSSSSSSSSSS");
             },
             error: function (xhr) {
                 //alert(xhr.responseText);
                 alert("error");
             }
         });
     }

我的網絡服務代碼

[WebMethod]
public void Send_Mail()
{
    try
    {
        string fromaddr = "frommail@gmail.com";
        string toaddr = "tomil@gmail.com";
        string bodytxt = "Body Message";
        string sub = "Subject Name";
        bodytxt = "Body Text";

        MailMessage mailreq = new MailMessage(fromaddr, toaddr);
        mailreq.IsBodyHtml = true;
        mailreq.Subject = sub;
        mailreq.Body = bodytxt;
        SmtpClient smtpreq = new SmtpClient();
        smtpreq.Send(mailreq);
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}

但是此代碼未輸入到Web服務功能代碼中。

任何幫助,將不勝感激。

您應該使用ScriptMethod屬性將public void Send_Mail()標記為。

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void Send_Mail()
{
    ...
}

也可能是您應該使用ScriptService屬性標記孔服務類。

您需要使用

類級別的[System.Web.Script.Services.ScriptService]和方法級別的[ScriptMethod(ResponseFormat=ResponseFormat.Json)]

此屬性將允許您訪問要從Javascript代碼調用的.asmx服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM