小程序利用WebService跟asp.net交互过程发现的问题并处理

最近在研究一个项目,用到asp.net跟小程序交互,简单的说就是小程序端利用wx.request发起请求。获取asp.net 响应回来的数据。但经常会报错。点击下图的测试按钮


出现如下错误:

百思不得其解,试了若干方法,都不行。
因为是第一次接触webservice,不是很精通,后来发现必须如下图所示点击asp.net中的运行才行


asp.net端点击运行代码状态如下

然后再点击小程序端的测试按钮,提示一切ok

显示数据可以正常获取了展开如下:

正确获取了asp.net 响应过来的数据,在上图中的data 中。贴出代码,供大家测试

小程序端代码如下:
wxml端:

<view class="container">
    <text class="user-motto">{{num}}</text>
    <button bindtap="requestWebService">测试</button>
</view>

小程序js端代码如下:

  /**
   * 页面的初始数据
   */
  data: {
    num:[]
  },
  //测试WebService
  requestWebService:function(){
    var that=this//注意这里必须缓存,不然无法在回调中
    wx.request({
      url: 'https://localhost:44346/test.asmx/Name',
      data: {
        a:1,
        b:2
      },
      method: 'POST', 
      success: function(res){
   
        console.log(res)
        that.setData({motto:res.data.d})//这里是that不是this
      },
      fail: function() {
       
      },
      complete: function() {
       
      }
    })
  },

asp.net端代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace 练习数据交互
{
    /// <summary>
    /// test 的摘要说明
    /// </summary>
    [WebService(Namespace = "练习数据交互")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    public class test : System.Web.Services.WebService
    {

        [WebMethod]
        public int[] Name(int a, int b)
        {
            return new int[] { a, b };
        }
    }
}

注意 [System.Web.Script.Services.ScriptService],不要注释。否则也会出错

相关推荐
2401_857622663 小时前
SpringBoot框架下校园资料库的构建与优化
spring boot·后端·php
正小安3 小时前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
2402_857589363 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
说私域4 小时前
基于定制开发与2+1链动模式的商城小程序搭建策略
大数据·小程序
哎呦没5 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
_.Switch5 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
杨哥带你写代码6 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
AskHarries7 小时前
读《show your work》的一点感悟
后端
A尘埃7 小时前
SpringBoot的数据访问
java·spring boot·后端
yang-23077 小时前
端口冲突的解决方案以及SpringBoot自动检测可用端口demo
java·spring boot·后端