I have these two select
s on a view:
我在视图上有这两个选择:
<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>
The first represents a State, and when I select it I want to execute the carregarCidades
function to load the cities of that stat and them load them in the other select. Here is the function:
第一个代表一个State,当我选择它时,我想执行carregarCidades函数来加载该stat的城市,然后将它们加载到另一个select中。这是功能:
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));
});
});
}
Now, here is the CarregarCidades action in the UsuarioController:
现在,这是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;
}
I'm debugging the action and apparently everything is ok:
我正在调试动作,显然一切正常:
But, after the Action returns the Json result, the callback funcion is not called on the jquery code and I got a 500 internal server error in my console.
但是,在Action返回Json结果之后,jquery代码上没有调用回调函数,我的控制台中出现了500内部服务器错误。
2 个解决方案
#1
4
You have to JsonAllowRequestbehavior
parameter to AllowGet
, by default it is DenyGet
:
您必须将JsonAllowRequestbehavior参数添加到AllowGet,默认情况下它是DenyGet:
JsonResult result = Json(dsC.Tables["curretorno"],JsonRequestBehavior.AllowGet);
You can read about Why it is needed on this post.
您可以在这篇文章中阅读有关为什么需要它的信息。
#2
1
I would first make sure your method has the [WebMethod]
attribute above its declaration.
我首先要确保您的方法在其声明之上具有[WebMethod]属性。
The second thing I would suggest is returning your Json like this:
我建议的第二件事就是像这样返回你的Json:
return Json(result, JsonRequestBehavior.AllowGet);
Generally it is one of or both of these issues that gives you a 500 error.
通常,这些问题中的一个或两个都会导致500错误。
Edit:
编辑:
Declaring it as a [WebMethod]
may not be necessary.
将其声明为[WebMethod]可能没有必要。
#1
4
You have to JsonAllowRequestbehavior
parameter to AllowGet
, by default it is DenyGet
:
您必须将JsonAllowRequestbehavior参数添加到AllowGet,默认情况下它是DenyGet:
JsonResult result = Json(dsC.Tables["curretorno"],JsonRequestBehavior.AllowGet);
You can read about Why it is needed on this post.
您可以在这篇文章中阅读有关为什么需要它的信息。
#2
1
I would first make sure your method has the [WebMethod]
attribute above its declaration.
我首先要确保您的方法在其声明之上具有[WebMethod]属性。
The second thing I would suggest is returning your Json like this:
我建议的第二件事就是像这样返回你的Json:
return Json(result, JsonRequestBehavior.AllowGet);
Generally it is one of or both of these issues that gives you a 500 error.
通常,这些问题中的一个或两个都会导致500错误。
Edit:
编辑:
Declaring it as a [WebMethod]
may not be necessary.
将其声明为[WebMethod]可能没有必要。