gpt4 book ai didi

javascript - 使用 jQuery $.ajax 调用另一个控件的操作

转载 作者:行者123 更新时间:2023-11-30 17:09:59 25 4
gpt4 key购买 nike

我有两个 Controller ,C1C2
C2 有一个名为 M2 的方法,它返回一个 JsonResult

现在我正在使用 $.ajax 调用从 View V1 调用方法 M2 但我猜想 C2/M2< 的相对路径/strong> 工作不正常。

$.ajax({
type: "GET",
url: "/C2/M2",
success: function (data) {
alert(data);
}
});

注意事项:
View V1 使用 Controller C1

呈现这个调用有什么问题?

public class C1 : Controller
{
public ActionResult Package(object Id)
{
return View("PackageO");
}
}

public class C2: Controller
{
public JsonResult SelectAll()
{
return Json(_rep.SelectAll(), JsonRequestBehavior.AllowGet);
}
}

jQueryready() View “PackageO”中,我正在使用 IIFE

$(function () {
var resultant = "";
$.ajax({
type: "GET",
url: "./C2/SelectAll",
//url: "@Url.Action('SelectAll', 'C2')",
done: function (data) {
alert("success");
},
fail: function (ex) {
alert("fail");
}
});
});

最佳答案

您应该永远不要MVC硬编码 URL

改为使用 @Url.Action

url: '@Url.Action("FunctionName", "ControllerName")',

如果这不能解决您的问题,请告诉我们您遇到了什么样的错误。

此外,为了您的理智,请使用 fail 方法。

$.ajax("http://url")
.done(function() {
alert("success");
})
.fail(function() {
alert("error");
})

最后一点,success 从 jQuery 1.8 开始被弃用;你应该使用 done 来代替。

关于javascript - 使用 jQuery $.ajax 调用另一个控件的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27234005/

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