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