在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>
Run Code Online (Sandbox Code Playgroud)
在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));
}
Run Code Online (Sandbox Code Playgroud)
结果是:
Object {d: null}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?如果我在Publicaciones带有值的变量的数据中发送参数"Noticia" …