Versiones:
Visual Studio 2008
jquery-1.4.1.min.js
System.Web.Extensions.dll versión 3.5.0.0
Código C#
using System;
using System.Web.Services;
namespace wsX
{
/// <summary>
/// Descripción breve de Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string Prueba1(int param)
{
return "{ 'datos': [ {'nombre': 'Juan', 'direccion': 'Colonia 1212'}, {'nombre': 'Pedro', 'direccion': 'Mercedes 1111'} ] }";
}
}
}
Código html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" language="javascript" src="jquery-1.4.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Prueba 1</title>
<body>
<script type="text/javascript">
$(document).ready(function()
{
jQuery.ajax({
type: "POST",
url: "http://localhost:2366/Service1.asmx/Prueba1",
data: "{'param': '123'}",
contentType:"application/json; charset=utf-8",
dataType:"json",
cache: false,
success: function(response) {
var v = getJSON(response);
alert("Resultado: " + v.datos[0].nombre);
}
});
function getJSON(data_obj)
{
if (data_obj.d)
data_ok = data_obj.d;
else
data_ok = data_obj;
evalJson = eval("var varJSON = " + data_ok + ";");
return varJSON;
}
});
</script>
</body>
</html>
No hay comentarios:
Publicar un comentario