noc*_*ist 2 javascript ajax asp.net-mvc
我对控制器中的 MVC ActionResult 进行了 AJAX 调用,试图返回一个布尔值。
我的 ajax 调用:
function CheckForExistingTaxId() {
$.ajax({
url: "/clients/hasDuplicateTaxId",
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
data: JSON.stringify({ taxId: taxId }),
});
}
Run Code Online (Sandbox Code Playgroud)
我的方法:(“clients”是默认路由前缀)
[HttpGet, Route("hasDuplicateTaxId")]
public ActionResult hasDuplicateTaxId(string taxId)
{
//if stuff
return Json(true, JsonRequestBehavior.AllowGet);
else
return Json(false, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
我想根据ajax调用的结果打开一个模态对话框:
if (CheckForExistingTaxId())
DialogOpen();
Run Code Online (Sandbox Code Playgroud)
第一个问题是我收到 404 Not Found for clients/hasDuplicateTaxId。我的路线或我调用它的方式有问题吗?其次,我是否能够以这种方式返回一个布尔值,在打开对话框之前使用 ajax 调用评估函数 CheckForExistingTaxId() ?
小智 5
基本上,如果想将 Json 用于HttpGet:
[HttpGet, Route("hasDuplicateTaxId")]
public ActionResult hasDuplicateTaxId(string taxId)
{
// if 1 < 2
return 1 < 2 ? Json(new { success = true }, JsonRequestBehavior.AllowGet)
: Json(new { success = false, ex = "something was invalid" }, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
阿贾克斯:
function CheckForExistingTaxId() {
$.ajax({
url: "/clients/hasDuplicateTaxId",
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
data: JSON.stringify({ taxId: taxId }),
success: function (data) {
if (data.success) {
// server returns true
} else {
// server returns false
alert(data.ex); // alert error message
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3283 次 |
| 最近记录: |