Место общения программистов C#
-
Anonymous
Получить JSON в ASHX AJAX C#
Сообщение
Anonymous »
У вас есть сценарий в Home.aspx:
Код: Выделить всё
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);
}
});
}
Внутри 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));
}
И результат:
Почему такой результат? если я отправлю параметр в данные с переменной Publicaciones со значением «Noticia».
Подробнее здесь:
https://stackoverflow.com/questions/223 ... ax-c-sharp
1736186740
Anonymous
У вас есть сценарий в Home.aspx:
[code]
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);
}
});
}
[/code]
Внутри Controlador.ashx:
[code]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));
}
[/code]
И результат:
[code]Object {d: null}
[/code]
Почему такой результат? если я отправлю параметр в данные с переменной Publicaciones со значением «Noticia».
Подробнее здесь: [url]https://stackoverflow.com/questions/22374316/get-json-in-ashx-ajax-c-sharp[/url]