一、微信小程序前端代码
1、index.wxml
<view>
{{student[0].name}}
</view>
<view>
{{student[0].sex}}
</view>
<view>
{{student[0].old}}
</view>
2、index.js
// index.js
// 获取应用实例
const app = getApp()
Page({
data: {
student: [{
"name": "张三",
"old": "15",
"sex": "女"
}]
},
// 事件处理函数
onLoad() {
wx.request({
url: 'http://localhost:3954/handle/ApiPost.ashx',
method: "post",
header: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8' // 默认值 x-www-form-urlencoded;charset=utf-8 json
},
data: {
id: 666,
name: "刘德华"
},
success: res => {
var resData = res.data.replace(" ", "");
//去掉utf8编码的BOM头
resData = resData.replace(/\ufeff/g, "");
var jsonObj = JSON.parse(resData);
console.log(jsonObj);
this.setData({
student: jsonObj
})
}
})
},
})
二、ASP.net后端代码
1、ApiPost.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ApiPost.aspx.cs" Inherits="ApiPost" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>接受和回传get/post参数</title>
</head>
<body>
接受和回传get/post参数
</body>
</html>
2、ApiPost.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ApiPost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
3、ApiPost.ashx
<%@ WebHandler Language="C#" Class="ApiPost_handle" %>
4、ApiPost.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
/// <summary>
/// TestHandler 的摘要说明
/// </summary>
public class ApiPost_handle : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string id = context.Request["id"];
string name = context.Request["name"];
string json1 = "[{\"name\":\""+name+"\","+
"\"old\":\"45\"," +
"\"sex\":\"男\"}]";
context.Response.Write(JsonConvert.SerializeObject(json1));
}
public bool IsReusable
{
get
{
return false;
}
}
}