当前位置:网站首页>【小程序】通过request实现小程序与后台asp.net的数据json传输(Post协议 图文+代码)
【小程序】通过request实现小程序与后台asp.net的数据json传输(Post协议 图文+代码)
2022-06-21 19:31:00 【敦厚的曹操】

一、微信小程序前端代码

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;
}
}
}
边栏推荐
- UIButton实现左文字右图片
- SQL教程之6种鲜为人知的 SQL 技术,可帮助您每月节省 100 小时
- Laravel imports and exports excel using phpoffice
- How functions are declared
- Shutter automatickeepaliveclientmixin cache component
- 零售数字化起锚阶段,更多地关注的是如何借助数字化的手段对流量进行挖掘和转化
- Introduction to internet protocol -- five layer model
- What is more advantageous than domestic spot silver?
- String类型转换成List<Integer>
- Data path: three people walk, there must be my teacher!
猜你喜欢

C语言回调函数到底是怎么回事?

Pfsense configuring tinc site to site tunneling tutorial

Shutter input box assembly

高性能内网DNS系统介绍

Highly scalable, emqx 5.0 achieves 100million mqtt connections

全新混合架构iFormer!将卷积和最大池化灵活移植到Transformer

Pfsense configurer le tutoriel de tunnel de site à site Tinc
![[CTF] attack and defense world Misc](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[CTF] attack and defense world Misc

M3608升压ic芯片High Efficiency 2.6A Boost DC/DC Convertor

拼多多618手机品牌官旗销量同比增长124%,4000+高价位手机同比增长156%
随机推荐
AXI_Bus_Matrix_4x4 设计 - 逻辑设计部分
History of the Great Game
pc 电商平台----search模块
LeeCode435 无重叠区间
延长 洁鲜生密实袋,给食材上一把“安全锁”
欢迎使用Markdown编辑器
Introduction to high performance intranet DNS system
最详细整理STL之vector基础
Class loading process of JVM
ASP.Net Core创建Razor页面上传多个文件(缓冲方式)
Unity analog flashlight light source detector, AI attack range detection area, object detection in visual cone, fan-shaped area detection, circular area detection, cone area detection
4.3寸触摸屏智能网络中控主机有哪些应用
Simulate the input and get the contents of print output
[CTF] attack and defense world Misc
Is it true and safe for qiniu to open a securities account? Do you charge for opening an account
启牛开通证券账户是真实安全的吗?开户收费吗
Shutter automatickeepaliveclientmixin cache component
How to use memcached to implement Django project caching
String类型转换成List<Integer>
Henkel database custom operator '! ~~'