ajax使用处理程序请求剃刀页面

时间:2018-04-05 02:23:02

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

我正在尝试使用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,但它显示整个网页作为响应。理想情况下它应该返回"新结果"在开发者工具的网络选项卡中。我在Pages中有Active.cshtml和Active.cshtml.cs会造成混淆吗?我该如何解决? 感谢

1 个答案:

答案 0 :(得分:2)

对于razor页面,您应该在querystring中传递处理程序方法的参数值。

这应该有用。

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);
    }
});