【问题标题】:ajax request to razor page using handler使用处理程序对剃须刀页面的 ajax 请求
【发布时间】:2018-09-14 17:35:44
【问题描述】:

我正在尝试使用 ajax 调用从 Active.cshtml.cs 文件中获取数据。 这是jquery代码:

var turl = '/Active?handler=Clients/' + id;
    $.ajax({
        type: "GET",
        url: turl,
        dataType: "json",
        success: function (result) {
           alert(JSON.stringify(result));
        });

这里是 Active.cshtml.cs 方法

public JsonResult OnGetClients()
        {
        return new JsonResult("new result");
        }

状态为 200 Ok,但它会显示整个网页作为响应。理想情况下,它应该在开发人员工具的网络选项卡中返回“新结果”。是不是我在页面中有 Active.cshtml 和 Active.cshtml.cs 会造成混乱?我该如何解决? 谢谢

【问题讨论】:

  • 这个问题是否与 ASP.NET MVC 中的剃须刀页面有关
  • 是的。它是关于如何处理 ajax 请求 om razor pages
  • 是asp.net core
  • .net core 2.0 ..
  • asp.net core razor page 你问对了

标签: c# jquery asp.net-mvc-4 razor mvvm


【解决方案1】:

对于剃须刀页面,您应该在查询字符串中传递处理程序方法的参数值。

这应该可行。

yourSiteBaseUrl/Index?handler=Clients&53

假设您的 OnGetClients 有一个 id 参数。

public JsonResult OnGetClients(int id)
{
    return new JsonResult("new result:"+id);
}

所以你的 ajax 代码应该是这样的

var id = 53;
var turl = '/Index?handler=Clients&id=' + id;
$.ajax({
    type: "GET",
    url: turl,
    success: function (result) {
        console.log(result);
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-18
    • 2019-08-13
    • 2021-06-29
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多