我正在使用jQuery验证插件。要检查用户名已经存在,我已经在我看来这样做:通过getJSON从视图调用ASP MVC 3控制器
$.validator.addMethod('usernameExists', function (value) {
var data = {};
data.username = $('#Username').val();
$.getJSON('/Account/CheckUsername', data, function (result) {
if (result.exists == 'true')
return true;
else
return false;
});
return false;
}, 'Username already exists.');
而在我的AccountController,我有:
public JsonResult CheckUsername(string username)
{
string test = "false";
return Json(test);
}
我已经把断点到“CheckUsername”和它永远不会到达那里,但它涉及到“getJSON”调用(我试过了)。有人能告诉我我做错了什么吗?显然,“getJSON”有些问题......但是什么?
尝试从URL中移除第一斜线:'$ .getJSON( '帐户/ CheckUsername' ...' – Mrchief
如果您没有不重新发明轮子,已经有内建[RemoteAttribute] (http://msdn.microsoft.com/en-us/library/system.web.mvc.remoteattribute(VS.98).aspx) – archil
@Mrchief:试过了,没有帮助。 – Alex