【发布时间】:2011-03-16 04:08:41
【问题描述】:
我没有从我的 WebService 数据中获取回来,我不知道为什么。 :/
我的 MasterPage.master 中有这个:
<asp:ScriptManager id="scriptMng" runat="server">
<Services>
<asp:ServiceReference Path="~/WebServices/Mailing.asmx" />
</Services>
</asp:ScriptManager>
<asp:UpdatePanel runat="server" id="mailFormUpdatePanel" updateMode="Conditional">
<ContentTemplate>
/* form */
</ContentTemplate>
</asp:UpdatePanel>
在我的 Mailing.asmx 中有这个:
<%@ WebService Language="C#" CodeBehind="~/App_Code/Services/Mailing.cs" Class="Mailing" %>
在我的 Mailing.asmx.cs 我有这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net.Mail;
using System.Text.RegularExpressions;
/// <summary>
/// Summary description for Mailing
/// </summary>
[WebService(Namespace = "http://ktwebstudio.cz/WebServices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 Mailing : System.Web.Services.WebService
{
[WebMethod()]
public string Mail(string fromName, string fromSurename, string fromEmail, string fromPhone, string phoneTimeFrom, string phoneTimeTo, string selectedJob, string mailMsg)
{
if (fromName.Trim() != "" && fromSurename.Trim() != "" && fromName != "Jméno" && fromSurename != "Příjmení" && fromEmail.Trim() != "" && fromEmail != "E-mailová adresa (povinné)")
{
bool error = false;
if (fromPhone.Trim() != "" && fromPhone != "Telefonní číslo (nepovinné)")
error = !IsNumber(fromPhone.Trim());
if (IsWord(fromName.Trim()) && IsWord(fromSurename.Trim()) && IsEmail(fromEmail.Trim()) && !error)
{
/*
some logic
*/
try { client.Send(mail); }
catch
{
return "Odesílání zprávy selhalo, zkuste prosím akci opakovat. <br />Při přetrvávajících problémech zkuste použít alternativní způsob kontaktování, který naleznetev sekci <a runat=\"server\" href=\"<%$RouteUrl:RouteName=contact%>\">kontakt</a>.";
}
return "Zpráva byla úspěšně odeslána, děkujeme.";
}
else
return "Vaše jméno, příjmení nebo jeden z Vášich vyplněných kontaktů obsahuje neplatné znaky.";
}
else
return "Musíte vyplnit Vaše jméno, příjmení a kontaktní email.";
}
public bool IsWord(string word)
{
Regex mask = new Regex(@"^[a-zA-Zá-žÁ-Ž]*$");
return mask.IsMatch(word);
}
public bool IsEmail(string email)
{
Regex mask = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", RegexOptions.IgnoreCase);
return mask.IsMatch(email);
}
public bool IsNumber(string number)
{
int num;
return int.TryParse(number, out num);
}
}
所以在我的 Masterpage.master.cs 中,我测试浏览器是否支持 JS,如果不支持,我会从 OnClick 方法调用 Web 服务,这是在哪里,我认为这没问题:(不要问我为什么如果 JS 我不想拥有这个不支持)
Mailing mail = new Mailing();
statusMsgLbl = mail.Mail(txtName.Text, txtSurename.Text, txtMail.Text, txtPhone.Text, startTime.SelectedValue, endTime.SelectedValue, selectJob.SelectedValue, txtMessage.Text);
但是如果支持 JS(我希望用户打开它)我在我的 Masterpage.master 中有这个 jQuery 和 javascript 代码:
var msg;
Sys.Application.add_load(load);
function load() {
$('#submit').click(function () {
msg = Mailing.Mail($('#txtName').val(), $('#txtSurename').val(), $('#txtMail').val(), $('#startTime').val(), $('#endTime').val(), $('#selectJob').val(), $('#txtMessage').val());
});
}
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandler);
function beginRequestHandler() {
$(document.createElement('div'))
.attr('id', 'overlay')
.width($('#formBox').width())
.height($('#formBox').height())
.css({ backgroundImage: 'url(/Static/Img/bc_overlay.png)', position: 'absolute', left: 0, top: 0, margin: "5px", textAlign: "center", color: "#000", display: "none" })
.append("<div id='loading' style='padding-top:100px'><strong>Odesílám</strong><br /><img src='Static/Img/ajax-loader.gif' width='33px' height='33px' alt='loading' /></div>")
.show(500)
.prependTo($('#formBox'));
$('#formBox').css('position', 'relative');
}
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
function endRequestHandler() {
$('#loading').delay(2000).slideUp(800);
$('#overlay').append("<div style='padding-top:100px'><strong id='statusMsg'></strong></div>");
if (msg != null)
$('#statusMsg').css('display', 'none').delay(2800).fadeIn(300).text(msg);
else
$('#statusMsg').css('display', 'none').delay(2800).fadeIn(300).text("Problém");
$('#overlay').delay(6000).hide(800);
}
第一个函数使用我的 WebService。
第二个函数在 beginRequest 时做一些很酷的事情。
最后一个函数在 endRequest + 写入错误/成功消息时也会做一些很酷的事情.. 但是我没有从 Mailing.asmx.cs 得到我的字符串,我不知道为什么。 :-/ 我放入最后一个函数 if(msg != null) ... 所以 我知道 msg 为 null 如果你能告诉我原因,我将不胜感激。
(我也在 jquery 中通过 $.ajax 尝试过,但我得到了 500 错误,所以我在 Sys.WebForms.PageRequestManager 中使用这种方式。)
【问题讨论】:
标签: jquery asp.net ajax web-services scriptmanager