当前位置:   article > 正文

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

asp.net 后端post请求 josn数据

 一、微信小程序前端代码

1、index.wxml

  1. <view>
  2. {{student[0].name}}
  3. </view>
  4. <view>
  5. {{student[0].sex}}
  6. </view>
  7. <view>
  8. {{student[0].old}}
  9. </view>

2、index.js

  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. student: [{
  7. "name": "张三",
  8. "old": "15",
  9. "sex": "女"
  10. }]
  11. },
  12. // 事件处理函数
  13. onLoad() {
  14. wx.request({
  15. url: 'http://localhost:3954/handle/ApiPost.ashx',
  16. method: "post",
  17. header: {
  18. 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' // 默认值 x-www-form-urlencoded;charset=utf-8 json
  19. },
  20. data: {
  21. id: 666,
  22. name: "刘德华"
  23. },
  24. success: res => {
  25. var resData = res.data.replace(" ", "");
  26. //去掉utf8编码的BOM头
  27. resData = resData.replace(/\ufeff/g, "");
  28. var jsonObj = JSON.parse(resData);
  29. console.log(jsonObj);
  30. this.setData({
  31. student: jsonObj
  32. })
  33. }
  34. })
  35. },
  36. })

二、ASP.net后端代码

 1、ApiPost.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ApiPost.aspx.cs" Inherits="ApiPost" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>接受和回传get/post参数</title>
  6. </head>
  7. <body>
  8. 接受和回传get/post参数
  9. </body>
  10. </html>

 2、ApiPost.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. public partial class ApiPost : System.Web.UI.Page
  7. {
  8. protected void Page_Load(object sender, EventArgs e)
  9. {
  10. }
  11. }

3、ApiPost.ashx

<%@ WebHandler Language="C#" Class="ApiPost_handle" %>

4、ApiPost.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Newtonsoft.Json;
  6. using System.IO;
  7. using Newtonsoft.Json.Linq;
  8. using Newtonsoft.Json;
  9. /// <summary>
  10. /// TestHandler 的摘要说明
  11. /// </summary>
  12. public class ApiPost_handle : IHttpHandler
  13. {
  14. public void ProcessRequest(HttpContext context)
  15. {
  16. context.Response.ContentType = "text/plain";
  17. string id = context.Request["id"];
  18. string name = context.Request["name"];
  19. string json1 = "[{\"name\":\""+name+"\","+
  20. "\"old\":\"45\"," +
  21. "\"sex\":\"男\"}]";
  22. context.Response.Write(JsonConvert.SerializeObject(json1));
  23. }
  24. public bool IsReusable
  25. {
  26. get
  27. {
  28. return false;
  29. }
  30. }
  31. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/147921
推荐阅读
相关标签
  

闽ICP备14008679号