2013-04-29 122 views
1

我有一个非常简单的JS-方法:控制器方法不叫

<script language="javascript"> 
    function AmountChanged(callingTextbox) {     
     var enteredQuantity = callingTextbox.value; 
     $.getJSON("/Catalog/GetEnteredQuantity", 
     { 
      id: enteredQuantity 
     }, 
     function (data) { 
      alert(data.MoneyText);     
     }); 
    } 
</script> 

这“应该”在我的控制器调用一个函数:

public partial class CatalogController : BaseController { 
    [HttpPost] 
    public JsonResult GetEnteredQuantity(object id) 
    { 
     var result = new { MoneyText = "kost nix" }; 
     return Json(result); 
    } 
} 

通过Chrome浏览器,我可以看到的是, JavaScript函数被调用。调试器逐步执行,直到线路$.getJSON("/Catalog/GetEnteredQuantity",,然后跳转到该JS函数的最后一个右括号。但GetEnteredQuantity() - 方法从不调用。

控制台显示一个"http://localhost:2451/Catalog/GetEnteredQuantity?id=48 404 Not Found"

这里有什么问题?

+2

那是因为你的AJAX调用生成取出HttpPost属性的'GET Request'和控制器配置为接受'POST requests',尝试删除'[HttpPost]'从您的控制器 – Drew 2013-04-29 09:51:12

+0

谢谢。而已。如果你写这个作为答案,我会标记它:) – 2013-04-29 10:00:03

回答

0

从动作

public partial class CatalogController : BaseController { 
    [HttpPost] //Remove it 
    public JsonResult GetEnteredQuantity(object id) 
    { 
     var result = new { MoneyText = "kost nix" }; 
     return Json(result); 
    } 
} 
+0

@Drew已经在评论中回答了这个(旧)问题。谢谢:) – 2014-01-27 09:54:45

+0

对不起,我错过了 – 2014-01-27 09:56:10