【发布时间】:2014-04-17 22:23:39
【问题描述】:
在 Home.aspx 中有一个脚本:
<script type="text/javascript">
function probarAjax() {
var Publicaciones = {
"Categoria": "Noticia"
}
$.ajax({
type: "POST",
url: "Controlador.ashx?accion=enviar",
data: JSON.stringify(Publicaciones),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
console.log(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
}
</script>
Controlador.ashx 内部:
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/json";
var categoria = string.Empty;
JavaScriptSerializer javaSerialize = new JavaScriptSerializer();
categoria = context.Request["Categoria"];
var capaSeguridad = new { d = categoria };
context.Response.Write(javaSerialize.Serialize(capaSeguridad));
}
结果是:
Object {d: null}
为什么会有这个结果?如果我在数据中发送一个参数,变量Publicaciones,值为"Noticia"。
【问题讨论】:
-
解决方案是这样的