jquery ajax call webservice: return json
View Code
<script type="text/javascript"> $(function () { $("#btn").click(function () { $.ajax({ type: 'POST', dataType: "json", contentType: 'application/json; charset=utf-8', url: '@Url.Content("~/services/WebService1.asmx/search")', data: "{ 'id': '1' }", success: function (data, textStatus, xhr) { var rows = $.map(jQuery.parseJSON(data.d), function (item) { return ('<tr><td>' + item.name + '</td></tr>'); }).join(''); $("#searchresults").html("<table>"+rows+"</table>"); }, error: function (xhr, textStatus, errorThrown) { alert("erropr"); } }); }); }); </script>
webservice return:
return "[{\"name\":\"this is name\",\"value\":\"This is value\"},{\"name\":\"this is name2\",\"value\":\"This is value2\"}]";
//if webservice return list<T> then use data,not data.d
jquery ajax call webservice . return xml
View Code
<script type="text/javascript"> $(function () { $("#btn").click(function () { $.ajax({ type: 'POST', dataType: "xml", contentType: 'application/json; charset=utf-8', url: '@Url.Content("~/services/WebService1.asmx/getDS")', data: "{ 'id': '1' }", success: function (data, textStatus, xhr) { try { $(xhr.response).find("Table1").each(function () { $('#searchresults').append($(this).find("ID").text() + " <-->" + $(this).find("Value").text()); }); } catch (e) { alert(e); return; } }, error: function (xhr, textStatus, errorThrown) { alert("erropr"); } }); }); }); </script>
webservice return:
View Code
[WebMethod] [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Xml)] public DataSet getDS(string id) { DataSet ds = new DataSet("table"); DataTable dt = new DataTable(); dt.Columns.Add("ID", Type.GetType("System.String")); dt.Columns.Add("Value", Type.GetType("System.String")); DataRow dr = dt.NewRow(); dr["ID"] = "1"; dr["Value"] = "你好1"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = "2"; dr["Value"] = "新年好"; dt.Rows.Add(dr); ds.Tables.Add(dt); return ds; }
jquery ajax call ashx:
View Code
<script type="text/javascript"> $(function () { $("#btn").click(function () { $.ajax({ type: 'POST', dataType: "json", contentType: 'application/json; charset=utf-8', url: '@Url.Content("~/services/WebService1.asmx/search")', data: "{ 'id': '1' }", success: function (data, textStatus, xhr) { var rows = $.map(data, function (item) { return ('<tr><td>' + item.name + '</td></tr>'); }).join(''); $("#searchresults").html("<table>"+rows+"</table>"); }, error: function (xhr, textStatus, errorThrown) { alert("erropr"); } }); }); }); </script>
ashx return:
View Code
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; JavaScriptSerializer json = new JavaScriptSerializer(); string value = json.Serialize(new Person { name = "aaa", age = "bbb" }); //context.Response.Write(value); //context.Response.Write("{'name': 'firstname', 'value': 'Hello'}"); context.Response.Write("[{\"name\":\"this is name\",\"value\":\"This is value\"},{\"name\":\"this is name2\",\"value\":\"This is value2\"}]"); context.Response.End(); }