Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery Javascript和MVC返回类型_Jquery_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

Jquery Javascript和MVC返回类型

Jquery Javascript和MVC返回类型,jquery,asp.net-mvc,asp.net-mvc-3,Jquery,Asp.net Mvc,Asp.net Mvc 3,我在ASP.Net MVC项目的一个页面中使用了以下JavaScript: function showAllianceMembers_onclick() { var strName = $("#allianceNameTextBox").val(); $.ajax( { type: "POST", url: "/Alliance/Index", data: "allianceName=" + strName,

我在ASP.Net MVC项目的一个页面中使用了以下JavaScript:

function showAllianceMembers_onclick() {
    var strName = $("#allianceNameTextBox").val();

    $.ajax(
    {
        type: "POST",
        url: "/Alliance/Index",
        data: "allianceName=" + strName,
        success: function (result) {
            if (result.success) {
                alert("done!");
            }
            else {
                alert("error!!");
            }
        },
        error: function (req, status, error) {
            alert(error);
        }
    });
}
如您所知,此脚本正在调用一个
MVC操作
。以下是MVC的动作:

[HttpPost]
public ActionResult Index(string allianceName)
{
    //Populating an object containing a list (IList)

    return View(res);
}
这里的问题是JavaScript代码只是显示带有错误消息的警报。。。
我的代码出了什么问题?

在控制器操作中,您发送的不是JSON,而是一个简单的视图。因此,
result
变量上没有定义
.success
属性。以下是您的AJAX请求的外观:

$.ajax({
    type: "POST",
    url: "/Alliance/Index",
    data: { allianceName: strName },
    success: function (result) {
        // If you got so far the AJAX request succeeded, the result variable
        // will contain the final HTML of the rendered view
        alert("done!");
    },
    error: function (req, status, error) {
        alert(error);
    }
});
或者,如果要从控制器操作发送JSON:

[HttpPost]
public ActionResult Index(string allianceName)
{
    // populate the result        
    return Json(new { success = true, result = res });
}
然后:

$.ajax({
    type: "POST",
    url: "/Alliance/Index",
    data: { allianceName: strName },
    success: function (result) {
        if (result.success) {
            // do something with result.res
            alert("done!");
        }
    },
    error: function (req, status, error) {
        alert(error);
    }
});

我假设您正在尝试检索数据,而不是HTML标记。
试试这个:

$.ajax(
{
    type: "POST",
    url: "/Alliance/Index",
    data: { 'allianceName' : strName },
    dataType: 'json',
    success: function (result) {
        if (result.success) {
            alert("done!");
        }
        else {
            alert("error!!");
        }
    },
    error: function (req, status, error) {
        alert(error);
    }
});
并更新以下内容:

[HttpPost]
public JsonResult Index(string allianceName)
{
    //Populating an object containing a list (IList)

    return new JsonResult { Data = res };
}

我是否应该将
ActionResult
更改为
JSonResult
?@TJ博士,不应该。Json方法返回一个JsonResult,该JsonResult又派生自ActionResult。为了更通用,请将ActionResult保留在您的操作签名中。他不会从他的操作返回JSON,而是返回一个HTML视图。啊,您已经更新了答案,以考虑我的评论,删除了我的否决票。@Darin,您不知道他实际上打算返回什么。我觉得这就是问题所在——他试图获取一些数据,但却返回了视图。是的,这就是为什么我在回答这个问题时提出了两种解决方案。当您回答您的问题时,您只是建议他使用JSON,但您没有表示也应该修改操作代码。您可能还想查看jQuery模板,对您从AJAX调用返回的数据进行客户端数据绑定到HTML表: