标签:uri script put client stat returns bag $.ajax length
/// <summary>
/// 显示
/// </summary>
/// <returns></returns>
public ActionResult get()
{
Uri url = new Uri("http://localhost:49903/");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/jaon"));
HttpResponseMessage message = client.GetAsync("api/Product").Result;
List<day19info> list = new List<day19info>();
if (message.IsSuccessStatusCode)
{
list = message.Content.ReadAsAsync<List<day19info>>().Result;
}
else
{
ViewBag.mess = "请求错误!";
}
return View(list);
}
/// <summary>
/// 新增
/// </summary>
/// <returns></returns>
public ActionResult ADD()
{
return View();
}
[HttpPost]
public ActionResult ADD(day19info s)
{
Uri url = new Uri("http://localhost:49903/");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpContent httpcontent = new StringContent("{‘name‘:\"" + s.name + "\",‘num‘:\"" + s.num + "\",‘prices‘:\"" + s.prices + "\"}");
httpcontent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
HttpResponseMessage message = client.PostAsync("api/Product",httpcontent).Result;
if (message.IsSuccessStatusCode)
{
ViewBag.mess = message.Content.ReadAsStringAsync().Result;
}
else
{
ViewBag.mess = "请求错误!";
}
return Content("<script>alert(‘添加成功!‘);location.href=‘/Home/get‘</script>");
}
/// <summary>
/// 修改
/// </summary>
/// <returns></returns>
public ActionResult upt()
{
return View();
}
[HttpPost]
public ActionResult upt(int id,day19info s)
{
Uri url = new Uri("http://localhost:49903/");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpContent httpcontent = new StringContent("{‘id‘:‘"+id+"‘,‘name‘:\""+s.name+"\",‘num‘:‘"+s.num+"‘,‘prices‘:\""+s.prices+"\" }");
httpcontent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
HttpResponseMessage message = client.PutAsync("api/Product/" + id, httpcontent).Result;
if (message.IsSuccessStatusCode)
{
ViewBag.mess = message.Content.ReadAsStringAsync().Result;
}
else
{
ViewBag.mess = "请求失败!";
}
return Content("<script>alert(‘修改成功!‘);location.href=‘/Home/get‘</script>");
}
/// <summary>
/// 删除
/// </summary>
/// <returns></returns>
public ActionResult del(int id)
{
Uri url = new Uri("http://localhost:49903/");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage message = client.DeleteAsync("api/Product/" + id).Result;
if (message.IsSuccessStatusCode)
{
ViewBag.mess = message.Content.ReadAsStringAsync().Result;
}
else
{
ViewBag.mess = "失败";
}
return Content("<script>alert(‘删除成功!‘);location.href=‘/Home/get‘</script>");
}
function chaxun()
{
var name = $("#name").val();
$.ajax({
url: "/chaApi/Product/cha/" + name,
type: "Get",
success: function (data) {
$("#body").empty();
var str = "";
for (var i = 0; i < data.length; i++) {
str += " <tr>";
str += "<td>" + data[i].id + "</td>";
str += "<td>" + data[i].name + "</td>";
str += "<td>" + data[i].num + "</td>";
str += "<td>" + data[i].prices + "</td>";
str += "<td><input type=\"button\" name=\"name\" value=\"del \" onclick=\"del("+data[i].id+")\" /><input type=\"button\" name=\"name\" value=\"Upt \" onclick=\"upt("+data[i].id+")\" /></td>";
str += " </tr>";
}
$("#body").append(str);
}
})
}
标签:uri script put client stat returns bag $.ajax length
原文地址:http://www.cnblogs.com/shenghuotaiai/p/7875248.html