Estoy utilizando .Net framework 2.0/jQuery para hacer una llamada Ajax a un servicio web 2.0. No importa en qué establezca el tipo de contenido en la llamada ajax, el servicio siempre devuelve XML. ¡Quiero que regrese Json!Cómo devolver JSON desde un servicio web 2.0 asmx
Aquí es la llamada:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "DonationsService.asmx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Hide the fake progress indicator graphic.
$('#RSSContent').removeClass('loading');
// Insert the returned HTML into the <div>.
$('#RSSContent').html(msg.d);
}
});
});
Esto es lo que ve el encabezado de la solicitud como en Fiddler:
POST /DonationsService.asmx/GetDate HTTP/1.1
x-requested-with: XMLHttpRequest
Accept-Language: en-us
Referer: http://localhost:1238/text.htm
Accept: application/json, text/javascript, */*
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; eMusic DLM/4; .NET CLR 2.0.50727)
Host: localhost:1238
Content-Length: 2
Connection: Keep-Alive
Pragma: no-cache
He intentado fijar el contentType a 'text/json' y obtener la misma resultados.
Aquí es el método de servicio Web:
<WebMethod()> _
Public Function GetDate() As String
'just playing around with Newtonsoft.Json
Dim sb As New StringBuilder
Dim sw As New IO.StringWriter(sb)
Dim strOut As String = String.Empty
Using jw As New JsonTextWriter(sw)
With jw
.WriteStartObject()
.WritePropertyName("DateTime")
.WriteValue(DateTime.Now.ToString)
.WriteEndObject()
End With
strOut = sw.ToString
End Using
Return strOut
End Function
y aquí es lo que devuelve:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://DMS.Webservices.org/">{"DateTime":"11/13/2008 6:04:22 PM"}</string>
¿Alguien sabe cómo forzar el servicio web para volver JSON cuando pido JSON?
Por favor, no me digan que actualice a .Net Framework 3.5 o algo por el estilo (no soy tan estúpido). Necesito una solución 2.0.
Quizás pueda usar mi solución; http://stackoverflow.com/questions/26140330/call-javascript-function-from-asmx – Erdogan
Puede probar mi solución; http://stackoverflow.com/questions/26140330/call-javascript-function-from-asmx – Erdogan