• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
向往
博客园    首页    新随笔    联系   管理    订阅  订阅
webservice 实现json模式

直接上代码

public string GetUserInfoByOpenid(string openid)
{
var weixinuser = new WeiXinUser();

weixinuser.NickName = user.NickName;
weixinuser.HeadImg = user.HeadPhoto;

var data = Newtonsoft.Json.JsonConvert.SerializeObject(weixinuser);
string callbackMethodName = HttpContext.Current.Request.Params["callback"] ?? "";

if (callbackMethodName == "")
{
return data;//非jsonp模式调用

}
else
{
string result = callbackMethodName + "(" + data + ");";//jsonp模式调用
HttpContext.Current.Response.Write(result);
HttpContext.Current.Response.End();

}
}
return "";
}

请注意 webconfig 需要配置  webservice 请求模式 get or post     jsonp是get模式

增加 <system.web> 

<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>

</system.web>

 

 

如何调用

 

$.ajax({
url: "http://xx/service/userservice.asmx/GetUserInfoByOpenid",
type: 'GET',
data:{openid:'ooo'},
dataType: 'jsonp',//here
success: function (data) {
alert(data.NickName)
}
});

完美收官

posted on 2015-08-06 12:31  向往  阅读(7385)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3