我正在学习网络开发。我只想在 ASP.Net MVC 应用程序中进行一个简单的 AJAX GET 调用,我想可视化它是如何工作的。但我不能这样做。我是初学者,我可能会犯任何愚蠢的错误。但截至目前,我的代码中没有错误。
以下是我所拥有的:
所以,我有一个已经加载的 Index.cshtml
文件。 Now in that page I want to make an Ajax GET call
to one of the function
that I have written in the HomeController
and action name is Test
。我只想在 Homecontroller 的那个 Test Action
中打断点,然后将一些东西返回给 AJAX 调用的 Success
。在 HomeController 中,我有以下操作
[HttpGet]
public ActionResult Test()
{
return View("hello");
}
查询
$.ajax({
url: '/Home/Test',
type: 'GET',
success: function (html) {
alert(html);
},
error: function (error) {
$(that).remove();
DisplayError(error.statusText);
}
});
}
困惑:我是否需要为 --- 创建一个 cshtml
Test
。但我其实不想那样。我只想要 Test
动作返回给我一个数据。我将在已打开的 Index.csthml
文件中显示该数据。
错误:没有错误,但我无法在控制器的 Test Action
中命中断点。请注意,AJAX 的 Success
正在命中,但我没有看到任何数据。但我确信它没有击中测试动作断点。
原文由 Unbreakable 发布,翻译遵循 CC BY-SA 4.0 许可协议
像这样更改您的操作方法
在“html”变量的成功方法中,您将返回“myContent”。