2014-03-27 42 views
0

我有mvc 4控制器方法通過使用AJAX方法獲取一些數據。不幸的是它不工作。我在成功和錯誤領域做了一些事情。我需要得到這個代碼中的錯誤。在MVC控制器的Ajax調用中不顯示警報

的Ajax方法:

$(function() { 
    $('#btnSave').on('click', function (event) { 
     event.preventDefault(); 
     debugger; 
     var country = $('#cnname').val(); 
     var customerID = $('#CustomerId').val(); 
     if (country == "United Kingdom") { 
      if (800 <= $('#SrcAmt').val() < 2000) { 
       $.ajax({ 
        type: 'GET', 
        url: '/CustomerIdentity/IdentityList', 
        data: { 'customerId': customerId }, 
        success: function (data) { 
         alert('ok'); 
        }, 
        error: function (event) { 
         alert('fail'); 
        } 
       }); 
      } 
     } 
    }); 
}); 

MVC控制器:

public JsonResult IdentityList(int customerId) 
     { 
      var customeridentity = db.CustomerIdentity.Include(c => c.Customer).Include(c => c.CountryIssued) 
       .Where(item => item.CustomerId == customerId); 
      if (customeridentity.Count() > 0) 
      { 
       return Json(true, JsonRequestBehavior.AllowGet); 
      } 
      else 
      { 
       return Json(false, JsonRequestBehavior.AllowGet); 
      } 
     } 
+0

檢查什麼樣的錯誤你是在控制檯 – Rex

+0

它沒有表現出..後法命中})獲得;在AJAX方法如何跟蹤的成功和錯誤 – user2710638

回答

0

我從來沒見過,你所面臨的問題之前。可能有3種問題:

1.邏輯錯誤 您是否完全檢查了代碼,並且是否已經調試過它?你有沒有意想不到的輸出 如果沒有,則轉到第二個錯誤。

2. HTTP方法 的類型,我建議你使用POST而不是GET,因爲我知道,你的一些數據發送到ASP的WebMethod。

3 ASPX文件 的路徑嘗試刪除反斜槓(/)爲/CustomerIdentity/IdentityList第一個字符,所以它應該是CustomerIdentity/IdentityList

怪異的情況是,當你的瀏覽器仍包含以前的javascript代碼,嘗試使用隱私瀏覽標籤來運行您的代碼。

0

您應該使用@ Url.Action(「行動,」控制器「),讓您的控制器動作的分辯道,其機會操作網址錯誤

在Ajax調用的url屬性做這樣的:

網址:Url.Action("action,"controller")