【小程序】通过request实现小程序与后台asp.net的数据json传输(Post协议 图文+代码)

本文展示了如何使用微信小程序进行前端展示,并通过调用ASP.NET后端API进行数据交换。在小程序中,代码显示了如何展示学生信息,并发送HTTP请求到后端接口。后端ASP.NET通过ApiPost.ashx处理请求,接收并返回JSON数据,实现了数据的传递和解析。
摘要由CSDN通过智能技术生成

 一、微信小程序前端代码

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;
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敦厚的曹操

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值