jquery,ajax,如何使得服务器返回json格式数据?
$.ajax({
url: "WebService.asmx/IsRegistered",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data:{_siteName:$('#tbSiteName').val(),_siteURL:$('#tbSiteURL').val()},
success: function(json) {
if(json.d=="true")
{
alert("ok");
}
}
}); });
用firebug查看,提示:
System.InvalidOperationException: 请求格式无效: application/json; charset=utf-8。
在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
本人想发送json格式数据,从服务器返回json格式数据,如何做到?
------解决方案--------------------
C# code
StringBuilder retVal = new StringBuilder();
retVal.Append("{list:[");
using (SqlDataReader reader = db.ExecuteReader(CommandType.Text, sql))
{
while (reader.Read())
{
retVal.Append("{");
for (int i = 0; i < reader.FieldCount; i++)
{
retVal.AppendFormat("{0}:'{1}'{2}", reader.GetName(i).Trim(), reader[i].ToString().Trim(), i == reader.FieldCount - 1 ? "" : ",");
}
retVal.Append("},");
}
}
retVal.Remove(retVal.Length - 1, 1);
retVal.Append("]}");
return retVal.ToString();