直接上代碼web
public string GetUserInfoByOpenid(string openid)
{
var weixinuser = new WeiXinUser();ajax
weixinuser.NickName = user.NickName;
weixinuser.HeadImg = user.HeadPhoto;json
var data = Newtonsoft.Json.JsonConvert.SerializeObject(weixinuser);
string callbackMethodName = HttpContext.Current.Request.Params["callback"] ?? "";post
if (callbackMethodName == "")
{
return data;//非jsonp模式調用
}
else
{
string result = callbackMethodName + "(" + data + ");";//jsonp模式調用
HttpContext.Current.Response.Write(result);
HttpContext.Current.Response.End();
}
}
return "";
}jsonp
請注意 webconfig 須要配置 webservice 請求模式 get or post jsonp是get模式url
增長 <system.web> get
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices> webservice
</system.web>string
如何調用it
$.ajax({
url: "http://xx/service/userservice.asmx/GetUserInfoByOpenid",
type: 'GET',
data:{openid:'ooo'},
dataType: 'jsonp',//here
success: function (data) {
alert(data.NickName)
}
});
完美收官