XHR2 和[FromBody]使用说明
2024-10-15 22:59:44
[FromBody]必须是application/json 否则会报415 不支持的类型
//Forms
function FormsPost(data) { //Default Type x-www-form-urlencoding =>Form
$.ajax({
url: "/api/AgentAccount/PostForm",
type: "post",
datatype: "json",
data: data,
success: function (data) {
alert('success');
}
}).fail(
function (xhr, textstatus, err) {
alert('error: ' + err);
});
} //[FromBody] Model
function ModelPost(data) {
$.ajax({
url: "/api/AgentAccount/UserRegister",
type: "post",
contentType: 'application/json; charset=utf-8',
datatype: "json",
data: JSON.stringify(data),//jSON.stringfy for Model transmit [FromBody] will be used;
success: function (data) {
alert('success');
}
}).fail(
function (xhr,textstatus, err) {
alert('error: ' + err);
});
} //[FromBody] String
function StringPost(data) {
data = { "": "sds" };
$.post('/api/AgentAccount/PostString', data);
$.ajax({
url: "/api/AgentAccount/PostString",
type: "post",
contentType: 'application/json; charset=utf-8',
datatype: "json",
data: data,
success: function (data) {
alert('success');
}
}).fail(
function (xhr, textstatus, err) {
alert('error: ' + err);
});
} //[FromBody] Image
function XhrUploadImage() { } //[FromeBody] xhr.Model
function XhrPostModel(data) {
xhr = new XMLHttpRequest();
xhr.open("POST", "/api/AgentAccount/UserRegister", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
xhr.onreadystatechange = function (event) {
if (4 == xhr.readyState) {
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
alert(xhr.responseText);
} else {
alert('error:' + xhr.status);
}
}
};
}
对应controller action
[HttpPost("PostForm")]
public JsonResult PostForm(string FullName)
{
var data = new
{
username = "PostString"
};
return Json(data);
} [HttpPost("UserRegister")]
public JsonResult UserRegister([FromBody]AspNetUsers value)
{
var data = new {
username="sf"
};
return Json(data);
} // POST api/values
[HttpPost("PostString")]
public JsonResult PostString([FromBody] string value)
{
var data = new
{
username = "sf"
};
return Json(data);
}
最新文章
- 用java单例模式实现面板切换
- java-阻塞队列
- LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2
- Linux常用性能调优工具索引
- php_mysqli面向对象链接数据库(一)
- html5media.js 让浏览器兼容<;Video>;<;Audio>; 标签
- [转载]DIV CSS设计时IE6、IE7、FF 与兼容性有关的特性
- wordpress 后台显示空白现象
- html5、css3及响应式设计入门
- API函数详解:API大全总目录(按字母排列)
- C++的变量初始化
- vue+element ui 表格自定义样式溢出隐藏
- C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压
- 为什么MySQL数据库索引选择使用B+树?
- OK6410移植linux3.3.1
- ASCII字符代码表
- CCObject
- DRUPAL8模版命名规则
- 把Model改成Lib
- 2017 Summary
热门文章
- python_way ,day7 面向对象 (初级篇)
- 在ubuntu上配置apue的运行环境
- 本地设置正常,放服务器上就报 System.Security系统找不到指定的文件解决方法
- react入门笔记
- 3----lua的数据转换及运算符
- jQuery 中的children()和 find() 的区别
- 操作符 Thinking in Java 第三章
- [转发] 老叶观点:MySQL开发规范之我见
- linux学习笔记2-命令总结2
- poj2074Line of Sight(直线相交)