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

完美收官


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM