js获取表单数据
var modelObj = {};
var modelFieldsArray = $('#AddMusicCategory').serializeArray();
$.each(modelFieldsArray, function () {
modelObj[this.name] = this.value;
});
var modelStr = JSON.stringify(modelObj);
var requestModel = {};
requestModel.method = "MusicCategoryCreate";
requestModel.modelstr = modelStr;
$.ajax({
type: "POST",
url: "../../ServiceCenter/Handler/BlogMusicHandler.ashx",
dataType: "json",
data: requestModel,
success: function (rep) {
if (rep.code == 0) {
layer.msg('保存成功', {
time: 1000
});
layer.close(index);
musicApp.musicCategoryMng.musicCategoryList();
}
else {
console.log(rep.code + ":" + rep.msg);
}
}
});
public class BlogMusicHandler : BaseHandler, IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string method = context.Request["method"];
switch (method)
{
case "MusicUpload":
this.MusicUpload(context);
break;
case "MusicList":
this.MusicList(context);
break;
case "MusicDelete":
this.MusicDelete(context);
break;
case "MusicUpdate":
this.MusicUpdate(context);
break;
case "MusicCategoryCreate":
this.MusicCategoryCreate(context);
break;
case "MusicCategoryList":
this.MusicCategoryList(context);
break;
case "MusicCategoryDelete":
this.MusicCategoryDelete(context);
break;
case "MusicCategoryUpdate":
this.MusicCategoryUpdate(context);
break;
case "MusicHomepage":
this.MusicHomepage(context);
break;
default:
break;
} context.Response.End();
} private void MusicUpload(HttpContext context)
{ } private void MusicList(HttpContext context)
{ } private void MusicDelete(HttpContext context)
{ }
private void MusicUpdate(HttpContext context)
{ }
/// <summary>
/// 新增音乐分类
/// </summary>
/// <param name="context"></param>
private void MusicCategoryCreate(HttpContext context)
{
try
{
LogWriter.ToTrace("开始调用新增音乐分类服务接口");
var strModel = context.Request.Params["modelstr"];
var model= JsonConvert.DeserializeObject<MusicCategoryModel>(strModel);
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"Name",model.Name},
{"ImgUrl",model.ImgUrl },
{"SortNo", (model.SortNo??0).ToString()}
});
HttpClient httpClient = GetUseHttpClient();
var apiResponse = httpClient.PostAsync("api/Music/CreateCategory", content).Result;
if (apiResponse.IsSuccessStatusCode)
{
var result = new { code = 0, msg = "新增音乐分类成功", data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
else
{
//webapi返回errcode errmsg
//104为远程webapi服务系统异常,否则为webapi服务业务异常
var receivedErrcode = apiResponse.Headers.GetValues("errcode");
var receivedErrmdg = apiResponse.Headers.GetValues("errmsg");
var errorCode = ((string[])receivedErrcode)[0];
var errorMsg = System.Web.HttpUtility.UrlDecode(((string[])receivedErrmdg)[0]);
if (errorCode == "104")
{
LogWriter.ToError("新增音乐分类服务接口响应结果异常(系统异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
else
{
LogWriter.ToError("新增音乐分类服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
var result = new { code = -1, msg = "新增音乐分类服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg, data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
}
catch (Exception ex)
{
LogWriter.ToError("新增音乐分类内部错误:" + ex.Message, ex);
}
LogWriter.ToTrace("结束调用新增音乐分类服务接口");
} private void MusicCategoryList(HttpContext context)
{
LogWriter.ToTrace("开始调用查询音乐分类列表服务接口"); HttpClient httpClient = GetUseHttpClient();
var apiResponse = httpClient.GetAsync("api/Music/CategoryList").Result;
if (apiResponse.IsSuccessStatusCode)
{
var responseValue = apiResponse.Content.ReadAsStringAsync().Result;
LogWriter.ToDebug("开始查询音乐分类列表->服务端返回数据:" + responseValue);
LogWriter.ToDebug("开始解析数据");
var serviceMusicCategoryModel = JsonConvert.DeserializeObject<List<MusicCategoryModel>>(responseValue);
LogWriter.ToDebug("解析数据成功");
var result = new { code = 0, msg = "获取数据成功", data = serviceMusicCategoryModel };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result)); }
else
{
//webapi返回errcode errmsg
//104为远程webapi服务系统异常,否则为webapi服务业务异常
var receivedErrcode = apiResponse.Headers.GetValues("errcode");
var receivedErrmdg = apiResponse.Headers.GetValues("errmsg");
var errorCode = ((string[])receivedErrcode)[0];
var errorMsg = System.Web.HttpUtility.UrlDecode(((string[])receivedErrmdg)[0]);
if (errorCode == "104")
{
LogWriter.ToError("查询音乐分类列表服务接口响应结果异常(系统异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
else
{
LogWriter.ToError("查询音乐分类列表服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
var result = new { code = -1, msg = "查询音乐分类列表服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg, data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
} private void MusicCategoryDelete(HttpContext context)
{
try
{
LogWriter.ToTrace("开始调用删除音乐分类服务接口");
var strModel = context.Request.Params["musicCategoryIds"];
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"musicCategoryIds",strModel}
});
HttpClient httpClient = GetUseHttpClient();
var apiResponse = httpClient.PostAsync("api/Music/DeleteCategory", content).Result;
if (apiResponse.IsSuccessStatusCode)
{
var result = new { code = 0, msg = "删除音乐分类成功", data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
else
{
//webapi返回errcode errmsg
//104为远程webapi服务系统异常,否则为webapi服务业务异常
var receivedErrcode = apiResponse.Headers.GetValues("errcode");
var receivedErrmdg = apiResponse.Headers.GetValues("errmsg");
var errorCode = ((string[])receivedErrcode)[0];
var errorMsg = System.Web.HttpUtility.UrlDecode(((string[])receivedErrmdg)[0]);
if (errorCode == "104")
{
LogWriter.ToError("删除音乐分类服务接口响应结果异常(系统异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
else
{
LogWriter.ToError("删除音乐分类服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
var result = new { code = -1, msg = "删除音乐分类服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg, data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
}
catch (Exception ex)
{
LogWriter.ToError("删除音乐分类内部错误:" + ex.Message, ex);
}
LogWriter.ToTrace("结束调用删除音乐分类服务接口");
}
private void MusicCategoryUpdate(HttpContext context)
{ } /// <param name="context"></param>
private void MusicHomepage(HttpContext context)
{
LogWriter.ToTrace("开始调用获取圈子音乐主页服务接口"); HttpClient httpClient = GetUseHttpClient();
var apiResponse = httpClient.GetAsync("api/Music/Homepage").Result;
if (apiResponse.IsSuccessStatusCode)
{
var responseValue = apiResponse.Content.ReadAsStringAsync().Result;
LogWriter.ToDebug("开始获取圈子音乐主页->服务端返回数据:" + responseValue);
LogWriter.ToDebug("开始解析数据");
var serviceMusicHomepageResponse = JsonConvert.DeserializeObject<MusicHomepageResponse>(responseValue);
LogWriter.ToDebug("解析数据成功");
var result = new { code = 0,msg = "获取数据成功", data = serviceMusicHomepageResponse };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result)); }
else
{
//webapi返回errcode errmsg
//104为远程webapi服务系统异常,否则为webapi服务业务异常
var receivedErrcode = apiResponse.Headers.GetValues("errcode");
var receivedErrmdg = apiResponse.Headers.GetValues("errmsg");
var errorCode = ((string[])receivedErrcode)[0];
var errorMsg = System.Web.HttpUtility.UrlDecode(((string[])receivedErrmdg)[0]);
if (errorCode == "104")
{
LogWriter.ToError("圈子音乐主页服务接口响应结果异常(系统异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
else
{
LogWriter.ToError("圈子音乐主页服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
var result = new { code = -1, msg = "圈子音乐主页服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg, data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
} public bool IsReusable
{
get
{
return false;
}
}
}
public class BaseHandler
{
private static readonly Dictionary<string, HttpClient> HttpClients = new Dictionary<string, HttpClient>();
private static readonly string circleRelationServiceName = "ZJCX.ZJX.CircleRelation.Service";
private static readonly object HttpClientObj = new object();
public static HttpClient GetUseHttpClient()
{
#if !DEBUG
string serviceUrl = ConfigHelper.GetBaseUrl(circleRelationServiceName);
#endif
#if DEBUG
string serviceUrl = "10.250.3.197:9313";
#endif if (!HttpClients.ContainsKey(serviceUrl))
{
lock (HttpClientObj)
{
if (!HttpClients.ContainsKey(serviceUrl))
{
string serviceBaseUrl = string.Format("http://{0}/", serviceUrl);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(serviceBaseUrl);
client.DefaultRequestHeaders.Connection.Add("keep-alive");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpClients.Add(serviceUrl, client);
}
}
}
return HttpClients[serviceUrl];
}
}
js获取表单数据的更多相关文章
- Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据
用servlet实现一个注册的小功能 ,后台获取数据. 注册页面: 注册页面代码 : <!DOCTYPE html> <html> <head> <meta ...
- ASP.NET MVC 获取表单数据
public class Person { public string Name{get;set;} public string Phone{get;set;} } view层 @model Mode ...
- 1.3(学习笔记)Servlet获取表单数据
一.Servlet获取表单数据 表单提交数据经由Servlet处理,返回一个处理结果显示在页面上, 那么如何获取表单提交的参数进出相应的处理呢? 主要用到以下方法: String getParame ...
- JS获取表单元素的value
<!-- 1.option selected属性,如果我们在下拉列表里面选择了一个option那么他的selected="true" ,如果我们想设置当前的option是选中 ...
- JSP简单练习-获取表单数据
在JSP中,server端程序与client交互最经常使用的方法就是採用表单提交数据.表单提交的方法主要有两种,一种是get方法.还有一种是post方法.两者最大的差别:使用get方法提交的数据会显示 ...
- JS--轻松设置获取表单数据
接触过Angularjs的都知道,ng支持双向绑定,我们可以轻轻松松的通过ngModel将我们的值绑定到界面,当修改了值提交表单的时候不需要再重新通过ID去重新抓取输入框信息了.那对于我们开发前台网站 ...
- php学习笔记-获取表单数据
在网页上经常要填写用户名和密码,点击确认按纽之后,用户名和密码经过前端处理之后发送到了服务器上,那么服务器端怎么获取到这些用户提交的数据呢?就是通过超级全局变量 _POST和_GET 先拿_POST做 ...
- Servlet 响应 响应相关与重定向 请求 获取表单数据2种方法
一.HttpServletResponse (响应) 包括下面三个: 1.响应消息行 HTTP/1.1 200 OK 200是HTTP状态码, 代表请求已成功. (查httpservletres ...
- FromData获取表单数据
一般想要不刷新页面提交数据时,可以使用ajax提交.如果数据量不大可以自己写json数据用ajax提交到后台服务,但是数据量多且需要动态添加数据时,自己写json格式数据就有点麻烦了,这时候就需要Fo ...
随机推荐
- WebForm 页面ajax 请求后台页面 方法
function ReturnOperation(InventoryID) { //入库 接口 if (confirm('你确认?')) { $.ajax({ type: "post&quo ...
- Tomcat服务器配置https协议(Tomcat HTTPS/SSL 配置)
通常商用服务器使用https协议需要申请SSL证书,证书都是收费的,价格有贵的有便宜的.它们的区别是发行证书的机构不同,贵的证书机构更权威,证书被浏览器否决的几率更小. 非商业版本可以通过keytoo ...
- Java程序(非web)slf4j整合Log4j2
一.依赖包准备 //slf4j项目提供 compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' //log4j2项目提供 co ...
- AESDK AE中层类型的3种取得方式
有一部分属于类型标志,比如调节层,空对象层.用mSuites->LayerSuite7()->AEGP_GetLayerFlags去取 而灯光,文字这些信息,直接取类型即可 mSuites ...
- CYQ多数据库链接
枚举名XXXEnum 对应XXXConn的配置数据库链接项 ,不多说,一看就明白!名字空间得要带上数据库名!
- C#之用XmlWriter保存XML数据
http://book.51cto.com/art/201012/241202.htm https://blog.csdn.net/hongkaihua1987/article/details/790 ...
- # mysqlbinlog mysql-bin.000004 mysqlbinlog: unknown variable 'default-character-set=utf8'
# mysqlbinlog mysql-bin.000004 mysqlbinlog: unknown variable 'default-character-set=utf8' 加上--no-def ...
- html-blogsdemo
博客标题小样,代码预览是有动态效果的,但在博客园发布就没动画了,知道的大神麻烦告知下,谢谢. code <!DOCTYPE html> <html lang="en&quo ...
- GDB和WinDbg中调用函数
GDB: 特别简单,直接写调用式子即可,如下图的p word.c_str(),其中word的类型是std::string WinDbg:目前都说是.call命令,说实话我宁愿不用...见: http: ...
- 安装CentOS版本的yum(转载)
安装CentOS版本的yum 下载源:http://mirrors.163.com/centos/6/os/i386/Packages/ 材料准备: python-iniparse-0.3.1-2.1 ...