webApi 数据绑定 获取

时间:2023-03-09 04:55:43
webApi 数据绑定 获取

直接上代码:

 <html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script>
function Test() {
/*
无参数Get $.ajax(
{
url: 'api/values/GetUser',
type: 'get',
//data:{},
success: function (data) {
console.log(JSON.stringify(data));
}
});
*/ /*
单个参数Get
*/
/*$.ajax(
{
url: 'api/values/GetName',
type: 'get',
data: {name:"pingdong"},
success: function (data)
{
console.log(JSON.stringify(data));
}
});
*/ /*Get多个参数Get
$.ajax(
{
url: 'api/values/GetNameAndSex',
type: 'get',
data: {name:'pingdong',sex:'男'},
success: function (data)
{
console.log(JSON.stringify(data));
}
});
*/ /*post 无参数
$.ajax(
{
url: 'api/values/PostNoParmer',
type: 'post',
data: {},
success: function (data) {
console.log(JSON.stringify(data));
}
}
);
*/ /*
post一个参数
$.ajax({
url: 'api/values/PostName',
type: 'post',
data: {"":'pingdong'},
success: function (data)
{
console.log(JSON.stringify(data));
}
});
*/ /*
post多个参数
$.ajax({
url: 'api/values/PostNameAndSex',
type: 'post',
data: {name:'pingdong',sex:'男'},
success: function (data)
{
console.log(JSON.stringify(data));
}
});
*/ /*
post json格式的数据(单条) var mydata = { name: "pingdong", sex: '男' }; $.ajax(
{
url: 'api/values/PostOneJson',
type: 'post',
data: JSON.stringify(mydata),//将mydata转化为字符串
contentType: 'application/json',//默认情况下会出错。因为ajax提交数据默认请求头中的Content-type为:application/x-www-form-urlencoded(即默认的key=value)。而我们需要提交的是json格式的数据,所以需要指定发送到服务端的数据格式:application/json
succsess: function (data)
{
console.log(data);
}
});
*/ /*
post json格式的数据(多条)
var mydata = [
{ name: "pingdong", sex: '男' },
{name:'张三',sex:'男'},
{name:'李四',sex:'男'},
];
$.ajax(
{
url: 'api/values/PostMoreJson',
type: 'post',
data: JSON.stringify(mydata),
contentType: 'application/json',
success: function (data)
{
console.log(JSON.stringify(data));
}
});
*/ /*传递复杂json(示例) var mydata = {
"productPrice": [
{
"priceInfo": [
{
"productid": "77",
"webstileid": "2"
},
{
"productid": "191",
"webstileid": "3"
}
]
},
{
"priceRegex": [
{
"webstileid": "2",
"tid": "6"
},
{
"webstileid": "3",
"tid": "7"
}
]
}
]
};
console.log(JSON.stringify(mydata));
$.ajax(
{
url: 'api/values/PostMoreAndMoreJson',
type: 'post',
data: JSON.stringify(mydata),
contentType: 'application/json',
success: function (data)
{
console.log(JSON.stringify(data));
}
});
*/
} /*
备注;
contentType:指定请求类型 "application/json"
dataType:指定输出类型 "xml"
*/ </script>
<title>Index</title>
</head>
<body>
<div>
<input type="button" onclick="Test()" value="点我" />
</div>
</body>
</html>
 using My_WebApiDemo.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json; namespace My_WebApiDemo.Controllers
{
// [Authentication]
public class ValuesController : ApiController
{ static List<User> userList=new List<User>{new User{ name="张三",password=""},
new User{ name="王五",password=""}};
public List<User> GetUser()
{
return userList;
} public string GetName(string name)
{
return string.Format("你好,我叫{0}.", name);
} public string GetNameAndSex(string name, string sex)
{
return string.Format("你好,我叫{0},性别:{1}", name,sex);
} public List<User> PostNoParmer()
{
return userList;
} /// <summary>
/// 单个参数获取的时候,并不是根据key=value,而是 =value
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string PostName([FromBody]string name)
{
return string.Format("你好,我叫{0}.", name);
} /// <summary>
/// 传递两个参数的时候,FromBody只能有一个,所以提供一个对象来获取对应的值
/// </summary>
/// <returns></returns>
public string PostNameAndSex(NameAndSex nameAndsex)
{
return string.Format("你好,我叫{0}.", nameAndsex.name,nameAndsex.sex);
} /// <summary>
/// 接收json格式的数据(单条)
/// </summary>
/// <returns></returns>
public string PostOneJson(NameAndSex nameAndsex)
{ return string.Format("你好,我叫{0}.", nameAndsex.name, nameAndsex.sex); } /// <summary>
/// 接收json格式的数据(多条)
/// </summary>
/// <returns></returns>
public string PostMoreJson(IList<NameAndSex> nameAndsexList)
{
string str = "";
foreach (var item in nameAndsexList)
{
str += string.Format("你好,我叫{0}.", item.name, item.sex);;
}
return str;
} /// <summary>
/// 接收json格式的数据(复杂)
/// </summary>
/// <returns></returns>
public string PostMoreAndMoreJson(Demo demo)
{
return JsonConvert.SerializeObject(demo);
} }
public class NameAndSex
{
public string name { get; set; }
public string sex { get; set; }
} public class Demo
{
public IList<productPrice> productPrice { get; set; }
} public class productPrice
{
public IList<priceInfo> priceInfo { get; set; }
public IList<priceRegex> priceRegex { get; set; }
} public class priceInfo
{
public string productid { get; set; }
public string webstileid { get; set; }
}
public class priceRegex
{
public string webstileid { get; set; }
public string tid { get; set; }
}
}