2013-04-24 62 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',

相關問題