Для этого я обычно перенаправляю данные в обработчик 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
Надеюсь, это может быть полезно для вас ...