javascript - Ajax 请求 asp.net mvc Controller 返回 500 - 内部服务器错误

标签 javascript c# jquery ajax asp.net-mvc

我在 View 上有这两个 select:

<select class="input-sm form-control input-s-sm inline" onchange="carregarCidades()" id="comboEstado">
    ...
</select>

<select class="input-sm form-control input-s-sm inline" id="comboCidade">
   ...
</select>

第一个代表一个州,当我选择它时,我想执行 carregarCidades 函数来加载该统计的城市,然后将它们加载到另一个选择中。这是函数:

function carregarCidades() {
    var url = "@Url.Action("CarregarCidades", "Usuario")";

    var estado = $("#comboEstado").find(":selected").text();

    $.get(url, { pEstado: estado }, function (cidades) {
        $("#comboCidade").html(""); // clear before appending new list 
        $.each(cidade, function (i, cidade) {
            $("#comboCidade").append(
                $('<option></option>').val(cidade.id_cidade).html(cidade.cidade));
        });
    });
}

现在,这是 UsuarioController 中的 CarregarCidades 操作:

public ActionResult CarregarCidades(string pEstado)
{
    string cCidades = oVFP.BuscaCidade(pEstado);

    DataSet dsC = new DataSet();
    dsC.ReadXml(new StringReader(cCidades));

    JsonResult result = Json(dsC.Tables["curretorno"]);
    return result;
}

我正在调试 Action ,显然一切正常:

enter image description here enter image description here

但是,在 Action 返回 Json 结果后,回调函数没有在 jquery 代码上调用,我在控制台中收到 500 内部服务器错误。

最佳答案

你必须 JsonAllowRequestbehavior AllowGet 的参数,默认为 DenyGet :

JsonResult result = Json(dsC.Tables["curretorno"],JsonRequestBehavior.AllowGet);

您可以阅读有关 Why it is needed on this post 的信息.

关于javascript - Ajax 请求 asp.net mvc Controller 返回 500 - 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36632615/

相关文章:

javascript - 如何使用 pouchdb 的索引一次性查询 X 个特定文档?

javascript - 如果值与数组显示项目中的另一个值匹配 JavaScript

javascript - 如何在给定的 Angular 应用程序中获取所有指令名称?

c# - asax 文件中的此上下文中的响应不可用

c# - 使用 iTextSharp 填写其他字段时,如何在 PDF 中强制格式化和计算?

javascript - 一般脚本 : selecting the input element for which keypress event is fired, 没有实际传递 ID 等

Javascript: move 以编程方式创建的图像不起作用

c# - ASP.NET MVC 上传文件

javascript - 无法将数组从 php 函数返回到 ajax 请求

javascript - 几秒钟后重新加载聊天的另一种方法