有时,我需要处理HTML唯一网站的联系表中的数据,并且具有托管限制,不能运行asp.net代码。
在这种情况下如何处理数据? (在简单的情况下,发送电子邮件)
答案 0 :(得分:0)
为此,我通常将数据重定向到asp.net处理程序(位于运行asp.net的另一台服务器上)。
这是我的方法:
html格式:
<form name="ajax-form" id="ajax-form" action="http://somedomain.com/handler.ashx" method="post">
<label for="name">
Nome: *
<span class="error" id="err-name">please enter your name</span>
</label>
<input name="name" id="name" type="text" />
<label for="email">
E-Mail: *
<span class="error" id="err-email">please enter your e-mail</span>
<span class="error" id="err-emailvld">the e-mail does not have a valid format</span>
</label>
<input name="email" id="email" type="text" />
<label for="message">Message:</label>
<textarea name="message" id="message"></textarea>
<div id="button-con"><button class="send_message" id="send">Send</button></div>
<div class="error text-align-center" id="err-form">Error on form. Please check!</div>
<div class="error text-align-center" id="err-timedout">Time out connecting to server...</div>
<div class="error" id="err-state"></div>
</form>
加载jquery后,我添加以下javascript:
jQuery(document).ready(function ($) {
$('#send').click(function(){
$('.error').fadeOut('slow');
var error = false;
var name = $('input#name').val();
if(name == "" || name == " ") {
$('#err-name').fadeIn('slow');
error = true;
}
var email_compare = /^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$/;
var email = $('input#email').val();
if (email == "" || email == " ") {
$('#err-email').fadeIn('slow');
error = true;
}else if (!email_compare.test(email)) {
$('#err-emailvld').fadeIn('slow');
error = true;
}
if(error == true) {
$('#err-form').slideDown('slow');
return false;
}
var obj = {};
obj.name = name;
obj.email = email;
obj.message = $("#message").val();
var jsonData = JSON.stringify(obj);
console.log(jsonData);
$.ajax({
type: "POST",
url: $('#ajax-form').attr('action'),
data: jsonData,
timeout: 6000,
error: function(request,error) {
if (error == "timeout") {
$('#err-timedout').slideDown('slow');
}
else {
$('#err-state').slideDown('slow');
$("#err-state").html('An error occurred: ' + error + '');
}
},
success: function() {
$('#ajax-form').slideUp('slow');
$('#ajaxsuccess').slideDown('slow');
}
});
return false;
});
});
最后,我的handler.ashx的代码:
<%@ WebHandler Language="VB" Class="Handler" %>
Imports System.Web.Script.Serialization
Imports System.IO
Public Class Handler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/plain"
Try
Dim strJson As String = New StreamReader(context.Request.InputStream).ReadToEnd()
Dim objUsr As userInfo = Deserialize(Of userInfo)(strJson)
If objUsr IsNot Nothing Then
Dim name As String = objUsr.name
Dim email As String = objUsr.email
Dim message As String = objUsr.message
'here you treat you data, some send email function using the data collected
context.Response.Write(String.Format("Name :{0} , email={1}, message={2}", name, email, message))
Else
context.Response.Write("No Data")
End If
Catch ex As Exception
context.Response.Write("Error :" & ex.Message)
End Try
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Class userInfo
Public Property name As String
Public Property email As String
Public Property message As String
End Class
Public Function Deserialize(Of T)(ByVal context As String) As T
Dim jsonData As String = context
Dim obj = CType(New JavaScriptSerializer().Deserialize(Of T)(jsonData), T)
Return obj
End Function
End Class
我希望这对您有用...