Ajax:前台 Get方法
$(document).ready(function () {
$("#button1").click(function () {
$.ajax({
type: "Get",
url: "/api/UserInfo/getway",
data: { "name":"Test"}
})
})
})
C#:后台
public string getway(string name)
{
string result = string.Empty;
result = "{Name:\"" + name + "\"}";
return result;
}
备注:Get方法实体类型传不进去,基础数据能传递进去,借助[FromUri]特性可以传递实体类,借助JSON.stringify()系列化与反系列化可以传递实体类。
Post 方法
$(document).ready(function () {
$("#FindProdcutDetail").click(function () {
$.ajax({
type: "Post",
url: "/api/UserInfo/getpost",
data: { "ProductCode": "JX00036","ProductName":"YaGao","ProductPrice":20.5}
})
})
})
[HttpPost]
public string getpost([FromBody]ProductDetial productDetial )
{
return "value";
}
//ProductDetial 实体类
public class ProductsDetial
{
private string productCode;
public string ProductCode
{
get { return productCode; }
set { productCode = value; }
}
private string productName;
public string ProductName
{
get { return productName; }
set { productName = value; }
}
private double productPrice;
public double ProductPrice
{
get { return productPrice; }
set { productPrice = value; }
}
}