微信小程序筆記

添加全局變量

在app.js文件中的globalData段中增加.

如下所示:

globalData: {

userInfo: null,

api:'https://www.xxxxxx.xxx/api/Account',

}

腳本提交post的代碼示例

var app = getApp()
Page({
  formSubmit: function (e) {
    var that = this;
    wx.request({
      //
      url: app.globalData.api ,
      data: {
        Name: e.detail.value.txtname,
        Password: e.detail.value.txtpwd
      },
      header: {
        'content-type': 'application/json'
      },
      method: "POST",
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            txtname: res.data.Result.Name
          })
          wx.showToast({
            title: '祝賀你,登錄成功!',//這裏打印出登錄成功
            icon: 'success',
            duration: 1000
          })
        } else {
          wx.showToast({
            title: '登錄失敗!',
            icon: 'loading',
            duration: 1500
          })
        }
      }
    })
  }
})

後臺代碼

 public class AccountController : ApiController
    {
        [HttpPost]
        public ApiResponse<UserLogin> Login(UserLogin user)
        {
     
            if (user.Name == "xiaoli" && user.Password == "123")
            {
                return new ApiResponse<UserLogin>
                {
                    Message = "登錄驗證成功",
                    Result = user
                };
            }
            else
            {
                return new ApiResponse<UserLogin>
                {
                    Code = 1,
                    Message = "登錄失敗",
                   
                };
            }

           
        }
    }


    public class ApiResponse<T>
    {
        public int Code { get; set; } = 0;
        public string Message { get; set; }
        public T Result { get; set; }
    }
    /// <summary>
    /// 賬號DTO
    /// </summary>
    public class UserLogin
    {
        public string Name { get; set; }
        public string Password { get; set; }
    }

    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章