c# webservice 小程序 开发遇到的坑

JQuery Ajax 在asp.net中使用总结

https://www.cnblogs.com/acles/articles/2385648.html

 

补充其他:

 Context.Response.ContentType = "text/plain";  //这种方式转中文
  Context.Response.Write(jss.Serialize(fruitlist)); //用这种方式将对象转成json 并以字符串返回,用Newtonjson也可以

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.Services;
//using Newtonsoft;
using System.Web.Script.Services;
using System.Web.Script.Serialization;


namespace WebApplication1
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
     [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
       // [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        //ResponseFormat:方法要返回的类型,一般为Json或者XML    
        //UseHttpGet:true表示前台的ajax是通过“Get”可以访问此方法,如果前台ajax通过POST,则报错
        //false表示前台的ajax是通过"post"来访问此方法,如果前台ajax通过get,则报错
        [WebMethod]
        public void  HelloWorld(string TYPE)
        {
            if (TYPE=="111")
            {
                List<fruit> fruitlist = new List<fruit>();
                fruit fuit1 = new fruit();
                fuit1.id = 1;
                fuit1.name = "百果园";
                fruitlist.Add(fuit1);
                fruit fuit2 = new fruit();
                fuit2.id = 2;
                fuit2.name = "千果园";
                fruitlist.Add(fuit2);
             //   string json = JsonConvert.SerializeObject(fruitlist);

                //return 返回的是xml格式
                //return "navList:[{id:1,name:'百果园'},{id: 2,name: '千果园'}]";
                //Context.Response.Charset = "GB2312"; //设置字符集类型  
                //Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                JavaScriptSerializer jss = new JavaScriptSerializer();
                Context.Response.ContentType = "text/plain";
                Context.Response.Write(jss.Serialize(fruitlist));
                Context.Response.End();  
               
            }
            else
            {
                //Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                Context.Response.Write("error");
                Context.Response.End();  
               
            }
        }
    }
}

小程序中调用webservice数据:

getData() {

    var that = this

    // 商品详情
    wx.request({
      url: 'http://localhost:81/WebService1.asmx/HelloWorld?TYPE=111',
      method: 'GET',
      data: {},
      header: {
        //设置参数内容类型为x-www-form-urlencoded
        'content-type': 'application/x-www-form-urlencoded', 
        'Accept': 'application/json'
      },
      success: function (res) {     
        that.setData({      
          navList: res.data
        })
      },
      fail: function (err) { 
        xmdmTotal=0;
      },//请求失败
      complete: function () { 
      }//请求完成后执行的函数
    })

  },

newtonjson使用:

1、添加引用 Newtonsoft.json.dll

2、using Newtonsoft.Json;

3、  string json1 = JsonConvert.SerializeObject(fruitlist);

 

小程序的问题:

1、如何将小程序 wx.request 调用success结果传递出去:

  用this?回调函数中不能用this,在外面定义

  var that = this

 然后 that.自定义的方法,将结果当做参数传出去自定义函数中使用

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第1章 C#编程基础  实例1 Hello C#!  实例2 HeyGuy  实例3 预定义类型  实例4 类型转换  实例5 选择语句  实例6 循环语句  实例7 跳转语句  实例8 数组  实例9 枚举与结构类型  实例10 类  实例11 位运算  实例12 操作符重载  实例13 虚方法  实例14 委托  实例15 属性  实例16 异常处理  实例17 链表  实例18 回文数  实例19 汉诺塔  实例20 冒泡排序  实例21 插入排序  小结 第2章 界面编程  实例22 Hello Form  实例23 主菜单  实例24 上下文菜单  实例25 工具栏  实例26 状态栏  实例27 进度条  实例28 滑块控件  实例29 单选框和复选框  实例30 列表框和组合框  实例31 列表视图  实例32 树视图  实例33 Timer控件  实例34 Splitter控件  实例35 时钟控件和日历控件  实例36 MDI窗口  实例37 窗体继承  实例38 自制控件  实例39 使用自制控件  实例40 模式与非模式  实例41 会跑的按钮  实例42 绘制背景  实例43 可调窗口  实例44 托动窗体  实例45 电子便条  实例46 计算器界面设计  实例47 计算器功能实现  实例48 小闹钟  小结 第3章 图形与图像处理  实例49 简单画图  实例50 使用OnPaint绘制图形  实例51 绘制贝赛尔曲线  实例52 显示图像  实例53 消除图片背景  实例54 底片滤镜  实例55 浮雕效果  实例56 显示字体  实例57 特效字  实例58 旋转图片  实例59 打印  小结 第4章 线程 第5章 文件处理和系统操作 第6章 数据库编程 第7章 网络编程 第8章 Web应用程序的开发 第9章 多媒体 第10章 安全性 第11章 设计模式 第12章 杂例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值