gpt4 book ai didi

javascript - mvc中用ajax调用controller

转载 作者:行者123 更新时间:2023-12-03 07:45:33 24 4
gpt4 key购买 nike

我正在尝试从 Controller 获取数据并使用 ajax 更新我的 View 。

这是我的 Controller :

public class PatientController
{
DatabaseContext db = new DatabaseContext();
public JsonResult GetPatientFromCpr()
{
var patient = db.Patients.FirstOrDefault(p => p.Cpr == "2410911615");
return new JsonResult() { Data = patient, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}

这是我的 ajax 调用:

    function getPatient() {
cpr2 = $("#cpr-first").val() + $("#cpr-last").val();
$.ajax(
{
url: '/Patient/GetPatientFromCpr',
dataType: 'json',
success: function () {
alert("success");
},
error: function () {
alert("error");
},
});
}

当我调用该函数时,我总是收到错误警报。

GET http://localhost:51140/Patient/GetPatientFromCpr 404 (Not Found)

谁能指出问题所在吗?

(编辑)添加“: Controller”后,我现在收到一个新错误

GET http://localhost:51140/Patient/GetPatientFromCpr 500(内部服务器错误)

最佳答案

您的“PatientController”不是Controller(它不是从Controller继承)

public class PatientController : Controller
{
....
}

关于javascript - mvc中用ajax调用controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35221564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com