.net+uniapp 前后端数据交互相关问题记录
uniapp 提交form表单
@submit | EventHandle | 携带 form 中的数据触发 submit 事件,event.detail = {value : {'name': 'value'} , formId: ''},report-submit 为 true 时才会返回 formId | |
---|---|---|---|
@reset | EventHandle | 表单重置时会触发 reset 事件 |
<form @submit="formSubmit" @reset="formReset">
<view class="uni-btn-v">
<button form-type="submit">Submit</button>
<button type="default" form-type="reset">Reset</button>
</view>
</form>
methods: {
formSubmit: function(e) {
console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
var formdata = e.detail.value
uni.showModal({
content: '表单数据内容:' + JSON.stringify(formdata),
showCancel: false
});
},
formReset: function(e) {
console.log('清空数据')
}
}
网络请求 uni.request
import $config from "@/appConfig.js"; //调用url前缀 *注意修改为自己的!!!
url: $config.SERVER_URL + 'api/sys/user/modify/uploadPic', //自行修改各自的对应的接口
参考链接
.net接受前端form表单的数据
//form 的提交方式是Get
request.QueryString[key]
//form 的提交方式是Post
string result = null;
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]);
}
}
public static string GetRequestString(string key, HttpRequestBase request)
{
string result = null;
if (request.Form.Get(key) != null && request.Form.Get(key).ToString() != "")
{
result = request.Form.Get(key).ToString();
}
else if (request.QueryString[key] != null && request.QueryString[key].ToString() != "")
{
result = request.QueryString[key].ToString();
}
else if (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;
}
.net向前端响应Json数据
- 创建一个结果响应的Model
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace demo3.Models
{
public class ResultModel
{
public string name
{
get; set;
}
}
}
- 设置响应的结果值,进行json序列化
public string getForm()
{
//设置响应结果
ResultModel res = new ResultModel();
res.name = "test";
//使用JSonConvert序列化
string rsp = JsonConvert.SerializeObject(res);
return rsp;
}
response 响应xml数据
string reMessage = "<?xml version='1.0'?><xml>"
+ "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName >"
+ "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName >"
+ "<CreateTime>" + CreateTime + "</CreateTime>"
+ "<MsgType><![CDATA[text]]></MsgType >"
+ "<Content><![CDATA[未找到相关内容]]></Content >"
+ "</xml>";
//创建XmlDocument对象,将符合xml格式的字符串加载到dom中
XmlDocument xdoc = new XmlDocument();
//string转xml
xdoc.LoadXml(reMessage);
Response.Write(xdoc.DocumentElement.OuterXml);
获取xml
//获取流
Stream requestStream = Request.InputStream;
byte[] requestByte = new byte[requestStream.Length];
requestStream.Read(requestByte, 0, (int)requestStream.Length);
//获取xml包
string temStr = Encoding.UTF8.GetString(requestByte);
//将xml文档加载到DOM中
XmlDocument doc = new XmlDocument();
doc.LoadXml(temStr);
//获取根节点
Debug.WriteLine(doc.DocumentElement.OuterXml);
//获取子节点
XmlNode xmlNode = doc.FirstChild;
if (xmlNode.HasChildNodes)
{
for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
{
Debug.WriteLine(xmlNode.ChildNodes[i].InnerText);
}
}
//获取最后一个子节点
Debug.WriteLine(xmlNode.LastChild.OuterXml);
//查找节点 xpath方式 或者 GetElementByID检索节点
XmlNode node = doc.DocumentElement.SelectSingleNode("FromUserName");
Debug.WriteLine(node.InnerText);
string openId = node.InnerText;
//获取ToUserName
string ToUserName = doc.DocumentElement.SelectSingleNode("ToUserName").InnerText;
string FromUserName = doc.DocumentElement.SelectSingleNode("FromUserName").InnerText;
string CreateTime = doc.DocumentElement.SelectSingleNode("CreateTime").InnerText;
string MsgType = doc.DocumentElement.SelectSingleNode("MsgType").InnerText;
string Content = doc.DocumentElement.SelectSingleNode("Content").InnerText;
string MsgId = doc.DocumentElement.SelectSingleNode("MsgId").InnerText;
发起get请求
//发送http get请求
string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
//创建请求
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentType = "application/json;encoding=utf-8";
//获取响应
WebResponse response = req.GetResponse();//未响应
Stream stream = response.GetResponseStream();
//解码数据流
StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8"));
string temString = reader.ReadToEnd();
var jObject = JObject.Parse(temString);
var access_token = jObject["access_token"].ToString();//获取time{"time":"2016-06-06 11:11:11"}
//JavaScriptSerializer s = new JavaScriptSerializer();
//Dictionary<string, string> dict = (Dictionary<string, string>)s.DeserializeObject(temString);
//string access_token = dict["access_token"];
Console.WriteLine(temString);
//关闭io流
reader.Close();
stream.Close();
发起post请求
//创建请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json;encoding=utf-8";
//request.ContentLength = Encoding.UTF8.GetByteCount(postData);
//#region 添加Post 参数(menu需要post的参数)
byte[] data = Encoding.UTF8.GetBytes(menu);
request.ContentLength = data.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
//获取响应
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
//解码
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
//响应结果
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
ViewBag.MyMessage = retString;
读取txt文件
//获取菜单内容
FileStream fs1 = new FileStream(Server.MapPath("~/") + "\\App_Data\\menu.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding("GBK"));
string menu = sr.ReadToEnd();
sr.Close();
fs1.Close();
字符串转json
string temString = getUserInfo_Access_token(code);
//转化为json字符串
var json = JObject.Parse(temString);
//获取access_token字段
string access_token = json["access_token"].ToString();