我正在使用jQuery调用Web服务以发送电子邮件(从用户反馈页面)。所有页面均为HTML。
我添加了一个名为"服务"的文件夹,并将Web服务文件放置在此处(easg.asmx和easg.asmx.cs)。当我从VS 2017中测试它时,它可以正常工作。
但是,当我发布并部署到服务器时,我会获得"在/services/easg.asmx上找到的Web服务"。
我可以运行localhost/weblity/services/easg.asmx,并且可以显示一种可用的方法。我不确定为什么从页面中的内容说找不到Web服务。
Web服务是应用程序的一部分,也是同一服务器上的一部分;这不是一个不同的项目。
这是(部分)代码:
.aspx页面:
<section id="contact" class="bg-light">
<div class="container">
<div class="row">
<div class="col-sm-12 ml-sm-auto col-md-10 pt-4">
<h1>Contact us</h1>
<form id="sendmail" method="POST" action="">
<div class="form-group">
<input type="email" class="form-control" id="inputEmail" aria-describedby="emailHelp" placeholder="Email Address">
</div>
<div class="form-group">
<input type="email" class="form-control" id="inputName" aria-describedby="nameHelp" placeholder="First and Last Name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="inputPhone" aria-describedby="phoneHelp" placeholder="Contact Number">
</div>
<div class="form-group">
<label for="inputMsg">Feedback/Questions</label>
<textarea class="form-control" id="inputMsg" rows="3"></textarea>
</div>
<button type="submit" id="btnSubmit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</section>
<script type="text/javascript">
$("#btnSubmit").click(function () {
debugger
$("#sendmail").validate();
if ($("#sendmail").valid()) {
$.ajax({
type: "POST",
url: "/services/easg.asmx/SendEmail",
cache: false,
contentType: "application/json; charset=utf-8",
data: "{ 'Msg': '" + $("#inputMsg").val() + "', " + "'To': 'someone@somewhere.com'," + "'From': '" + $("#inputEmail").val() + "'," + "'Name': '" + $("#inputName").val() + "'," + "'Subject': 'Guide Feedback'," + "'Phone': '" + $("#inputPhone").val() + "'" + "}",
dataType: "json",
complete: function (transport) {
alert(transport.status);
if (transport.status == 200) {
alert('Your message was sent; thank you for your feedback');
}
else {
alert("Failed to send feedback; please try again later. Status Code: " + transport.status);
}
}
});
}
//so that the page doesn't post back
return false;
});
</script>
Web服务位于服务 easg.asmx.cs:
namespace EAStyleGuide
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class easg : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
private void SendEmail(string Msg, string To, string From, string Name, string Subject, string Phone)
{
using (var message = new MailMessage())
{
message.From = new MailAddress(From);
message.To.Add(new MailAddress(To));
message.Subject = Subject;
message.Body = GetMailBody(From, Msg, Phone, Name);
message.Priority = MailPriority.Normal;
message.IsBodyHtml = false;
SmtpClient sc = new SmtpClient("mailrelay.blah.com");
sc.Credentials = new NetworkCredential();
try
{
sc.Send(message);
}
catch (SmtpFailedRecipientsException FRE)
{
}
catch (SmtpException smtpEx)
{
}
catch (Exception generalEx)
{
}
}
return;
}
我必须更改URL:"/services/Easg.asmx/sendemail",将其更改为url:" services/easg.asmx/sendemail"(在"服务"之前摆脱后扣。