直接上代码
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)
}
});
完美收官
转自:https://www.cnblogs.com/waitingfor/p/4707669.html