直接上代码

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)
}
});

完美收官

相关文章:

  • 2021-11-08
  • 2021-10-08
  • 2021-05-21
  • 2021-09-14
  • 2021-04-08
  • 2021-09-20
  • 2022-01-19
  • 2022-12-23
猜你喜欢
  • 2021-12-19
  • 2022-12-23
  • 2021-10-03
  • 2022-01-05
相关资源
相似解决方案