1. 程式人生 > 實用技巧 >uniapp提交資料相關問題

uniapp提交資料相關問題

uniapp提交資料相關問題記錄

1、uni.request提交資料

uni.request({
    method:"POST",
    data:{
        code:"0000",
        message:"操作成功"
    },
    url: this.$baseUrl.baseUrl+'ModifyPswd/modifyPasswordTips',
    success : (res) =>{
    	
	},
    fail : (res) =>{
        
    }
})

向後臺提交資料,可以得到相應的響應,但是無法進行跳轉頁面

2、使用js,前端生成form表單進行提交

let url = this.$baseUrl.baseUrl+'ModifyPswd/modifyPasswordTips';
var data = {"code":"0000","message":"操作成功"};
this.postUrl(url,"POST",data);
/*
* url:跳轉連結
* method:跳轉方式
* params:傳遞引數   [{name:"test" ,data:"123"}]
* */
postUrl(URL,METHOD,PARAMS) { 
    var tmp_form = document.createElement("form");      
    tmp_form.action = URL; 
    tmp_form.method = METHOD;
    // form.enctype = "multipart/form-data";
    tmp_form.style.display = "none";
    for (var x in PARAMS) {
        var opt = document.createElement("input");
        opt.name = x;
        opt.value = PARAMS[x];
        tmp_form.appendChild(opt);
    }
    document.body.appendChild(tmp_form);
    tmp_form.submit(); 
    document.body.removeChild(tmp_form);
} 

自定義的js函式,動態建立form表單,向後臺提交資料,可以進行後臺的頁面跳轉,但是無法進行獲取相應的響應

3、C# 相關後端程式碼

        public ActionResult modifyPasswordTips()
        {
            string code = GetRequestString("code");
            string message = GetRequestString("message");

            //string reUrl = WxConfig.CallbackUrl + "/wxfront/index.html";
            //Session["path"] = "person/healthcard/healthcard";

            Session["path"] = "Views/Begin/index.html#/pages/index/MotifyTips/MotifyTips";
            string url = "{0}/{1}?code={2}&message={3}";
            //url = string.Format(url, WxConfig.CallbackUrl, Session["path"], "0000", "操作成功");
            url = string.Format(url, WxConfig.CallbackUrl, Session["path"], code, message);

            base.Response.Redirect(url,false);
            return View("/Views/Begin/Index.cshtml");//對映其他頁面的檢視
        }

        private string GetRequestString(string key)
        {
            string result = null;
            if (base.Request.Form.Get(key) != null && base.Request.Form.Get(key).ToString() != "")
            {
                result = base.Request.Form.Get(key).ToString();
            }
            else if (base.Request.QueryString[key] != null && base.Request.QueryString[key].ToString() != "")
            {
                result = base.Request.QueryString[key].ToString();
            }
            else if (base.Request.HttpMethod.ToUpper() == "POST")
            {
                try
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream, Encoding.UTF8, true, 2048, true))
                    {
                        string inputStream = sr.ReadToEnd();
                        JObject jsonObj = JObject.Parse(inputStream);
                        if (jsonObj != null)
                        {
                            result = Convert.ToString(jsonObj[key]);
                        }

                        Request.InputStream.Position = 0;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.LogError("從Post引數中獲取資料出現異常", ex);
                    return null;
                }

            }
            return result;
        }

    }