zoukankan      html  css  js  c++  java
  • 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)
    }
    });

    完美收官

  • 相关阅读:
    5.8
    python运维自动化
    javascript学习(一)
    python学习-1
    A-GPS学习笔记(二) 之SUPL
    A-GPS学习笔记(一)
    CF756D Bacterial Melee
    LG P2495 [SDOI2011]消耗战
    LG P7325 [WC2021] 斐波那契
    LG P7324 [WC2021] 表达式求值
  • 原文地址:https://www.cnblogs.com/waitingfor/p/4707669.html
Copyright © 2011-2022 走看看