62,270
社区成员
发帖
与我相关
我的任务
分享 var p = {
body: body,
total_fee: total_fee,
auth_code: auth_code
};
jQuery.ajax({
url: ServerUrlPrefix() + 'GetReceivedStatus.ashx',
type: 'POST',
data: JSON.stringify(p),
dataType: 'text',
......
或者 var p = {
body: body,
total_fee: total_fee,
auth_code: auth_code
};
jQuery.ajax({
url: ServerUrlPrefix() + 'GetReceivedStatus.ashx',
type: 'POST',
data: p,
dataType: 'json',
......这类形式。
对于第一种,适合于直接在服务器端使用 using (var st = new StreamReader(context.Request.InputStream, Encoding.UTF8))
{
var input =st.ReadToEnd();
............
这种形式提交数据,它可以提交任意嵌套深度、比较复杂的对象数据。
对于第二种,你可以在服务器端使用普通的 req.Form["id"] 之类的方式来获得一组 post 字段数值,可以提交比较简单的对象数据。 function GetUserInfoByOpenId(id, callback, error) {
if (error === void 0) { error = null; }
var url = “http://www.abc.com/weixin/GetUserInfo.ashx?openid=' + id;
jQuery.ajax({
url: url,
type: 'GET',
dataType: 'json',
success: function (res) {
callback(res);
},
error: function (e) {
if (error != null)
error(e);
}
});
}
这里使用异步方式,返回了一个具有如下接口模式
interface UserInfo
{
subscribe: number,
openid: string,
nickname: string,
sex: number,
language: string;
city: string;
province: string,
country: string,
headimgurl: string,
subscribe_time: number,
unionid: string,
remark: string,
groupid: number
}
的 javascript 对象。
在ashx中,你可能这这样写using System.Web;
using WxPayAPI;
namespace My微信网站
{
public class GetUserInfo : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var req = context.Request;
var openid = req.QueryString["openid"];
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN";
WeixinUserInfo usr = 查询微信公众平台获取用户信息(string.Format(url, weixinConfig.ACCESS_TOKEN, openid));
result = JsonConvert.SerializeObject(usr); //将 .net 对象序列化
context.Response.Write(result);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
这样,一个 WeixinUserInfo 类型的实体对象,在浏览器端就转换为一个 javascript 对象(使用变量res引用)。



