Ajax跨域调用的范例
Ajax的写法:
$(function() { var oid = GetRequest("OID"); //oid = "gh_fbcc787818d3"; var openid = GetRequest("OPENID"); //openid = "oVPTzt1-KZwUAP5xw8Vv1CaofMbQ"; if (oid != "") { $.ajax({ url : "http://it.livzon.cn/WXManage/WXAjax.ashx", type : "get", async : false, // dataType: "text", dataType : "jsonp", jsonp : "callbackparam",//服务端用于接收callback调用的function名的参数 jsonpCallback : "success_jsonpCallback",//callback的function名称 data : { flag : "GETWXUSERBYWXOIDANDOPENID", oid : oid, openid : openid }, success : function(response) { response = eval(response); var str = ""; if (response["name"] != "") { $("#username").val(response["name"]); $("#password").val(response["pwd"]); // $("#password").val("1111"); } //$('#submit').click(); }, error : function() { alert("获取微信用户信息失败!"); } }); } });
后台代码的处理:
string openid = context.Request["openid"]; string oid = context.Request["oid"]; List<WXUserModel> list = GetWXUserByWXIDAndOpenIDAndOID(0, 0, openid, oid); context.Response.ContentType = "text/plain"; String callbackFunName = context.Request["callbackparam"]; if (list.Count > 0) { byte[] bytes = Convert.FromBase64String( list[0].ADPASSWORD ); string pwd = Encoding.Default.GetString(bytes); context.Response.Write(callbackFunName + "({ \"name\": \"" + list[0].ADACCOUNT + "\", \"pwd\": \"" + pwd + "\"})"); } else { context.Response.Write(callbackFunName + "({ \"name\": \" \", \"pwd\": \" \"})"); }