【发布时间】:2015-06-29 12:54:40
【问题描述】:
// 网络服务代码 webService.asmx 这是从网络服务获取数据的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Configuration;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getTableData()
{
SqlConnection _connect = new SqlConnection();
try
{
_connect.ConnectionString = ConfigurationManager.ConnectionStrings["ERPV2ConnectionString"].ToString();
_connect.Open();
string query = "Select FirstName [Name], EmpCode [Code] From payroll.tblEmpMaster where EmpNo between 12 And 15";
SqlCommand cmd = new SqlCommand(query, _connect);
string strquery = null;
cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
_connect.Close();
strquery = DataSetToJSON(ds);
return strquery;
}
catch (Exception ex)
{
_connect.Close();
throw ex;
}
}
public string DataSetToJSON(DataSet ds)
{
StringBuilder JsonString = new StringBuilder();
DataTable dt = ds.Tables[0];
//Exception Handling
if (dt != null && dt.Rows.Count > 0)
{
JsonString.Append("[ ");
for (int i = 0; i < dt.Rows.Count; i++)
{
JsonString.Append("{ ");
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j < dt.Columns.Count - 1)
{
JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() +
"\":" + "\"" +
dt.Rows[i][j].ToString() + "\",");
}
else if (j == dt.Columns.Count - 1)
{
JsonString.Append("\"" +
dt.Columns[j].ColumnName.ToString() + "\":" +
"\"" + dt.Rows[i][j].ToString() + "\"");
}
}
/*end Of String*/
if (i == dt.Rows.Count - 1)
{
JsonString.Append("} ");
}
else
{
JsonString.Append("}, ");
}
}
JsonString.Append("]");
return JsonString.ToString();
}
else
{
return null;
}
}
}
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script>
<script>
$(document).ready(function () {
$("button").click(function () {
$.ajax({ type: "POST",
contenttype: "application/json; charset=utf-8",
data: "{null}",
url: "WebService.asmx/getTableData",
dataTyp: "json",
success: function (data) {
var xml = $(data);
$("#table1").append("<tr><th>name</th><th>code</th></tr>");
$.each(xml, function (i, v) {
// $('#p1').html(v.city);
$("#table1").append("<tr><td>" + v.name + "</td> <td>" + v.code + "</td></tr>");
});
},
error: function (err) {
alert(err);
}
});
});
});
</script>
发送
我在这段代码中做错了什么?
【问题讨论】:
标签: c# jquery asp.net web-services