[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);
}

  

 

最新文章

  1. 用java单例模式实现面板切换
  2. java-阻塞队列
  3. LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2
  4. Linux常用性能调优工具索引
  5. php_mysqli面向对象链接数据库(一)
  6. html5media.js 让浏览器兼容&lt;Video&gt;&lt;Audio&gt; 标签
  7. [转载]DIV CSS设计时IE6、IE7、FF 与兼容性有关的特性
  8. wordpress 后台显示空白现象
  9. html5、css3及响应式设计入门
  10. API函数详解:API大全总目录(按字母排列)
  11. C++的变量初始化
  12. vue+element ui 表格自定义样式溢出隐藏
  13. C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压
  14. 为什么MySQL数据库索引选择使用B+树?
  15. OK6410移植linux3.3.1
  16. ASCII字符代码表
  17. CCObject
  18. DRUPAL8模版命名规则
  19. 把Model改成Lib
  20. 2017 Summary

热门文章

  1. python_way ,day7 面向对象 (初级篇)
  2. 在ubuntu上配置apue的运行环境
  3. 本地设置正常,放服务器上就报 System.Security系统找不到指定的文件解决方法
  4. react入门笔记
  5. 3----lua的数据转换及运算符
  6. jQuery 中的children()和 find() 的区别
  7. 操作符 Thinking in Java 第三章
  8. [转发] 老叶观点:MySQL开发规范之我见
  9. linux学习笔记2-命令总结2
  10. poj2074Line of Sight(直线相交)