如何通过ajax调用控制器 [英] How do call a controller by ajax

查看:56
本文介绍了如何通过ajax调用控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过ajax调用控制器我确定我在我的javascript中使用它我的网址看起来像这样 - > url:

 ' <%:Url.Action(Contact,HomeController )%>'





但是当我调试它没有调用控制器时,任何人都可以帮助我的完整资源,



  if (error ==  false ){
$ .ajax({
type: POST
url:' <%:Url.Action(Contact, HomeController)%>'
数据:data_string,
超时: 6000
错误:功能(请求,错误){
if (error == timeout){
$(' #contact-error')。slideDown(' 慢');
$(' #contact-error span')。text(' 联系服务器时超时。');
setTimeout(function(){
$(' #contact-error' ).slideUp(' slow');
}, 10000 );
}
其他 {
$(' #contact-error')。slideDown(' slow' );
$(' #contact-error span')。text(' 有些东西不起作用。请再试一次。');
setTimeout(function(){
$(' #contact-error' ).slideUp(' slow');
}, 10000 );
}
},
成功:function(){
$(' #contact-success')。slideDown(' slow');
$(' #contact-success span')。text(' 发送消息。');
setTimeout(function(){
$(' #contact-success' ).slideUp(' slow');
}, 10000 );
$(' #name')。val(' ');
$(' #contactNumber')。val(' ');
$(' #message')。val(' ');
}
});
} else {
$(' #接触错误)隐藏();
$(' #contact-success')。hide();
}





这是我的控制器



< pre lang =c#> [HttpPost]
public ActionResult联系人( string name,< span class =code-keyword> string contactNumber, string message)
{
尝试
{
var appSettings = new AppSettingsReader( );
var emailAddress = appSettings.GetValue( ToEmailAddress typeof string ))。ToString();

var mail = new MailMessage {Subject = Digitali};
mail.To.Add(emailAddress);
mail.Body = string .Format( 姓名:{0}< br />联系电话:{1}< br />< br />消息:{2},姓名,contactNumber,消息);
var mailClient = new SmtpClient();
mailClient.Send(mail);
}
catch (异常例外)
{

}
return View();
}

解决方案

.ajax({
type: POST
url:' <%:Url.Action(Contact,HomeController)%>'
data:data_string,
timeout: 6000
错误:函数(请求,错误){
if (error == timeout){


' #contact-error')。slideDown(' 慢');


' #contact-error span')。text(' 联系服务器时超时。);
setTimeout(function(){


How do call a controller by ajax i'm certain i have it right in my javascript my url looks like this --> url:

'<%: Url.Action("Contact", "HomeController") %>',



But when im debuging its not calling the controller can anyone help heres my full source,

if (error == false) {
        $.ajax({
            type: "POST",
            url: '<%: Url.Action("Contact", "HomeController") %>',
            data: data_string,
            timeout: 6000,
            error: function(request,error) {
                if (error == "timeout") {
                    $('#contact-error').slideDown('slow');
                    $('#contact-error span').text('Timed out when contacting server.');
                    setTimeout(function() {
                        $('#contact-error').slideUp('slow');
                    }, 10000);
                }
                else {
                    $('#contact-error').slideDown('slow');
                    $('#contact-error span').text('Something is not working. Please try again.');
                    setTimeout(function() {
                        $('#contact-error').slideUp('slow');
                    }, 10000);
                }
            },
            success: function() {
                $('#contact-success').slideDown('slow');
                $('#contact-success span').text('Message sent.');
                setTimeout(function() {
                    $('#contact-success').slideUp('slow');
                }, 10000);
                $('#name').val('');
                $('#contactNumber').val('');
                $('#message').val('');
            }
        });
    } else {
        $('#contact-error').hide();
        $('#contact-success').hide();
    }



And here is my controller

[HttpPost]
        public ActionResult Contact(string name, string contactNumber, string message)
        {
            try
            {
                var appSettings = new AppSettingsReader();
                var emailAddress = appSettings.GetValue("ToEmailAddress", typeof(string)).ToString();

                var mail = new MailMessage { Subject = "Digitali" };
                mail.To.Add(emailAddress);
                mail.Body = string.Format("Name: {0} <br/> Contact number: {1} <br/><br/> Message: {2} ", name, contactNumber, message);
                var mailClient = new SmtpClient();
                mailClient.Send(mail);
            }
            catch (Exception exception)
            {
                
            }
            return View();
        }

解决方案

.ajax({ type: "POST", url: '<%: Url.Action("Contact", "HomeController") %>', data: data_string, timeout: 6000, error: function(request,error) { if (error == "timeout") {


('#contact-error').slideDown('slow');


('#contact-error span').text('Timed out when contacting server.'); setTimeout(function() {


这篇关于如何通过ajax调用控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆