using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.UI.MobileControls;
using SG_Soft_SqlDAL;
using System.Web.Script.Services;
/// <summary>
/// Hello 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class Hello : System.Web.Services.WebService
{ public Hello()
{ //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public string HelloWorld()
{
return "Hello World";
}
/// <summary>
/// 查询类别的返回参数
/// </summary>
/// <returns>总记录数</returns>
[WebMethod]
public string GetCategoryCount()
{
CategoryMan cm = new CategoryMan();
int count = cm.GetCategoryCount();
return count.ToString();
}}$.ajax({
url:"Hello.asmx/GetCategoryCount",
type:"POST",
dataType:"json",
contentType:"application/json;charset=utf-8",
success:function(msg){
var json = null;
try
{
json = eval('('+msg+')');
}catch(e)
{
alert("返回字符串不是JSON格式。");
return;
}
alert(msg);
},
error:function(err,status)
{
alert(status);
}
});返回的error信息:parsererror,在网上找了一中午的帖子,愣是没找到是什么原因。。
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.UI.MobileControls;
using SG_Soft_SqlDAL;
using System.Web.Script.Services;
/// <summary>
/// Hello 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class Hello : System.Web.Services.WebService
{ public Hello()
{ //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public string HelloWorld()
{
return "Hello World";
}
/// <summary>
/// 查询类别的返回参数
/// </summary>
/// <returns>总记录数</returns>
[WebMethod]
public string GetCategoryCount()
{
CategoryMan cm = new CategoryMan();
int count = cm.GetCategoryCount();
return count.ToString();
}}$.ajax({
url:"Hello.asmx/GetCategoryCount",
type:"POST",
dataType:"json",
contentType:"application/json;charset=utf-8",
success:function(msg){
var json = null;
try
{
json = eval('('+msg+')');
}catch(e)
{
alert("返回字符串不是JSON格式。");
return;
}
alert(msg);
},
error:function(err,status)
{
alert(status);
}
});返回的error信息:parsererror,在网上找了一中午的帖子,愣是没找到是什么原因。。
你对一个服务的调用,返回的对象都是强类型的。
而楼主却将可以返回强类型的东西却返回json这种纯文本格式。完全是本末倒置...我的看法是写一个ashx,然后返回json.
速度比web服务及aspx页都要快。