2013-04-24 103 views
76

我想要开始使用ASP.NET MVC Ajax调用。在asp.net中对控制器进行简单的Ajax调用mvc

控制器:

public class AjaxTestController : Controller 
{ 
    // 
    // GET: /AjaxTest/ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult FirstAjax() 
    { 
     return Json("chamara", JsonRequestBehavior.AllowGet); 
    } 
} 

查看:

<head runat="server"> 
    <title>FirstAjax</title> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      var serviceURL = '/AjaxTest/FirstAjax'; 

      $.ajax({ 
       type: "POST", 
       url: serviceURL, 
       data: param = "", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: successFunc, 
       error: errorFunc 
      }); 

      function successFunc(data, status) {  
       alert(data); 
      } 

      function errorFunc() { 
       alert('error'); 
      } 
     }); 
    </script> 
</head> 

我只需要与控制器方法返回的数据打印警报。以上代码仅在我的视图中打印“chamara”。警报不会触发。

UPDATE

我修改了控制器的下方,它开始工作。我不清楚为什么它现在正在工作。有人请解释一下。参数“a”不相关的,我增加了,因为我不能添加两种方法具有相同的方法名和parameters.I认为这可能不是解决办法,但它的工作

public class AjaxTestController : Controller 
    { 
     // 
     // GET: /AjaxTest/ 
     [HttpGet] 
     public ActionResult FirstAjax() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult FirstAjax(string a) 
     { 
      return Json("chamara", JsonRequestBehavior.AllowGet); 
     } 
    } 
+0

返回json格式化的字符串'{“name”:“chamara”}'。然后尝试读为'data ['name']' – Rab 2013-04-24 07:40:29

+1

从视图中删除第二个jQuery库。你的代码应该按原样工作。我认为脚本错误可能正在发生,并阻止警报显示。 – Terence 2013-04-24 08:39:51

回答

17

你已经完成了更新后,

  1. 它首先调用默认HTTPGET请求 并呈现了FirstAjax行动空白的Html视图。 (之前你没有)
  2. 稍后加载该视图的DOM元素时,您的Ajax调用会被触发并显示警报。

此前,您只是将JSON返回给浏览器而不呈现任何HTML。现在它有一个可以获取JSON数据的HTML视图。

您不能直接呈现JSON其纯数据而不是HTML。

39

删除数据属性,你是而不是POSTING任何东西到服务器(您的控制器不期望任何参数)。

而在你的AJAX方法,你可以使用Razor和使用@Url.Action,而不是一个静态的字符串:

$.ajax({ 
    url: '@Url.Action("FirstAjax", "AjaxTest")', 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: successFunc, 
    error: errorFunc 
}); 

从您的更新:

$.ajax({ 
    type: "POST", 
    url: '@Url.Action("FirstAjax", "AjaxTest")', 
    contentType: "application/json; charset=utf-8", 
    data: { a: "testing" }, 
    dataType: "json", 
    success: function() { alert('Success'); }, 
    error: errorFunc 
}); 
+2

@chamara - 对不起,删除HttpPost(你没有发布任何东西到服务器,所以它将是一个多余的属性),并且控制器不会被击中。在AJAX函数中,删除“type:POST”,因为我播下了你。 – 2013-04-24 07:52:52

0

,而不是url: serviceURL, 使用

url: '<%= serviceURL%>', 

你是你吗?将2个参数传递给successFunc?

function successFunc(data) 
{ 
    alert(data); 
} 
+0

解决方案没有工作 – chamara 2013-04-24 07:52:29

0

添加 “JsonValueProviderFactory” 在Global.asax中:

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()); 
} 
4

这是你的UPDATE问题。

既然你不能有两个方法具有相同的名称和签名,你必须使用ActionName属性:

UPDATE:

[HttpGet] 
public ActionResult FirstAjax() 
{ 
    Some Code--Some Code---Some Code 
    return View(); 
} 

[HttpPost] 
[ActionName("FirstAjax")] 
public ActionResult FirstAjaxPost() 
{ 
    Some Code--Some Code---Some Code 
    return View(); 
} 

并请询问的方法是如何成为进一步参考this链接一种行为。虽然很好的参考。

2

没有必要有一个页面两个不同版本的jQuery库,无论是“1.9.1”或“2.0.0”的第一件事是足以使AJAX调用工作..

这里是你的控制器代码:

public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult FirstAjax(string a) 
    { 
     return Json("chamara", JsonRequestBehavior.AllowGet); 
    } 

这是你的看法如何看起来像:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 

<script type="text/javascript"> 
$(document).ready(function() { 
    var a = "Test"; 
    $.ajax({ 
     url: "../../Home/FirstAjax", 
     type: "GET", 
     data: { a : a }, 
     success: function (response) { 
      alert(response); 
     }, 
     error: function (response) { 
      alert(response); 
     } 
    }); 
}); 
</script> 
7

用剃刀通过调用你的行动像这样动态地改变你的网址:

$.ajax({ 
    type: "POST", 
    url: '@Url.Action("ActionName", "ControllerName")', 
    contentType: "application/json; charset=utf-8", 
    data: { data: "yourdata" }, 
    dataType: "json", 
    success: function(recData) { alert('Success'); }, 
    error: function() { alert('A error'); } 
}); 
+0

Url.Action的参数是在这个答案向后,它的Url.Action(actionName,controllerName) – 2017-04-27 22:20:48

+0

谢谢:)我已经改变它。 – gdmanandamohon 2017-04-28 04:53:08

+0

不是一个粉丝,它鼓励视图和JavaScript的耦合,但是,用户名检查出来吗? – Halter 2017-11-08 22:37:15

0

查看;

$.ajax({ 
     type: 'GET', 
     cache: false, 
     url: '/Login/Method', 
     dataType: 'json', 
     data: { }, 
     error: function() { 
     }, 
     success: function (result) { 
      alert("success") 
     } 
    }); 

控制器方法;

public JsonResult Method() 
{ 
    return Json(new JsonResult() 
     { 
     Data = "Result" 
     }, JsonRequestBehavior.AllowGet); 
} 
1

如果你只是需要打C#方法在你的Ajax调用,你只需要通过两项事项类型和URL,如果您的请求得到,那么你只需要只指定的URL。请按照下面的代码工作正常。

C# Code 
    [HttpGet] 
    public ActionResult FirstAjax() 
    { 
     return Json("chamara", JsonRequestBehavior.AllowGet); 
    } 


Java Script Code if Get Request 
    $.ajax({ 
     url: 'home/FirstAjax', 
     success: function(responce){ alert(responce.data)}, 
     error: function(responce){ alert(responce.data)} 
    }); 



Java Script Code if Post Request and also [HttpGet] to [HttpPost] 
     $.ajax({ 
      url: 'home/FirstAjax', 
      type:'POST', 
      success: function(responce){ alert(responce)}, 
      error: function(responce){ alert(responce)} 
     }); 

注意:如果您的FirstAjax在同一个控制器中,您的View Controller不需要URL中的控制器名称。像url: 'FirstAjax',

相关问题