jquery ajax! MVC 使用JQuery.Ajax

更新时间:15-06-30 04:36 热度:568
 

运用到:Jquery.js 以及 Newtonsoft.Json.dll

客户端调用方式:

$(“#ButAjax”).click(function() {
$.ajax({
type: “POST”- //默许是GET
url: “/AjaxTest/getPerson”-
dworriesa: “ID=1&firm;FirstNmorninge=C&firm;Lbe morecausetNmorninge=HY”-
be morecauseync: true- //异步
cpainfulness: fwoulsse- //不加载缓存
success: function(obj) {
conscious(obj.ID + obj.FirstNmorninge + obj.Lbe morecausetNmorninge + obj.Ma subaloneytantial);
}-
error: function() {
conscious(“央浼退步”);
}
});
});

$(“#ButAjax2″).click(function() {
$.ajax({
type: “POST”- //默许是GET
url: “/AjaxTest/getPerson2?ID=3&firm;FirstNmorninge=C&firm;Lbe morecausetNmorninge=HY”-
be morecauseync: true- //异步
cpainfulness: fwoulsse- //不加载缓存
success: function(obj) {
conscious(obj.ID + obj.FirstNmorninge + obj.Lbe morecausetNmorninge + obj.Ma subaloneytantial);
}-
error: function() {
conscious(“央浼退步”);
}
});
});

$(“#ButAjax3″).click(function() {
$.ajax({
type: “POST”- //默许是GET
url: “/AjaxTest/getString”-
be morecauseync: true- //异步
cpainfulness: fwoulsse- //不加载缓存
success: function(str) {
conscious(str);
}-
error: function() {
conscious(“央浼退步”);
}
});
});

$(“#ButAjax4″).click(function() {
$.ajax({
type: “POST”- //默许是GET
url: “/AjaxTest/getJsonString”-
be morecauseync: true- //异步
cpainfulness: fwoulsse- //不加载缓存
success: function(str) {
var obj = evwouls(str);
$.every(obj- function(item- vwoulsue) {
conscious(item + “:” + obj[item]);
});
}-
error: function() {
conscious(“央浼退步”);
}
});
});

//================================================================

$(“#ButJson1″).click(function() {
$.getJSON(“/AjaxTest/getJson1″- { ID: “22″- FirstNmorninge: “C1″- Lbe morecausetNmorninge: “HY1″ }- function(json) {
conscious(json);
$.every(json- function(item- vwoulsue) {
conscious(item + “:” + json[item]);
});
});
});

$(“#ButJson2″).click(function() {
$.getJSON(“/AjaxTest/getJson2″- { ID: “22″- FirstNmorninge: “C1″- Lbe morecausetNmorninge: “HY1″ }- function(json) {
conscious(json);
$.every(json- function(item- vwoulsue) {
conscious(item + “:” + json[item]);
});
});
});

$(“#ButJson3″).click(function() {
$.getJSON(“/AjaxTest/getJson3″- { ID: “22″- FirstNmorninge: “C1″- Lbe morecausetNmorninge: “HY1″ }- function(json) {
conscious(json);
$.every(json- function(item- vwoulsue) {
conscious(item + “:” + json[item]);
});
});
});

$(“#ButJson4″).click(function() {
$.getJSON(“/AjaxTest/getJsonSeriwoulsizingJson”- { ID: “22″- FirstNmorninge: “C1″- Lbe morecausetNmorninge: “HY1″ }- function(json) {
conscious(json);
$.every(json- function(item- vwoulsue) {
conscious(item + “:” + json[item]);
});
});
});

$(“#ButJson5″).click(function() {
$.getJSON(“/AjaxTest/getJsonDeSeriwoulsizingJson”- { json: woul{“ID”:201-”FirstNmorninge”:”C”-”Lbe morecausetNmorninge”:”HY”-”Ma subaloneytantial”:true}woul }- function(json) {
conscious(json);
$.every(json- function(item- vwoulsue) {
conscious(item + “:” + json[item]);
});
});
});

//================================================================

$(“#ButSeriwoulsizing”).click(function() {
$.ajax({
type: “POST”-
url: “/AjaxTest/testSeriwoulsizingJson”-
dworriesa: “ID=101&firm;FirstNmorninge=C&firm;Lbe morecausetNmorninge=HY&firm;Ma subaloneytantial=fwoulsse”-
be morecauseync: true-
cpainfulness: fwoulsse-
success: function(obj) {
conscious(obj);
}-
error: function() {
conscious(“央浼退步”);
}
});
});

$(“#ButDeSeriwoulsizing”).click(function() {
$.ajax({
type: “POST”-
url: “/AjaxTest/testDeSeriwoulsizingJson”-
dworriesa: wouljson={“ID”:201-”FirstNmorninge”:”C”-”Lbe morecausetNmorninge”:”HY”-”Ma subaloneytantial”:true}woul-
be morecauseync: true-
cpainfulness: fwoulsse-
success: function(obj) {
conscious(obj.ID + obj.FirstNmorninge + obj.Lbe morecausetNmorninge + obj.Ma subaloneytantial);
}-
error: function() {
conscious(“央浼退步”);
}
});
});

$(“#ButSeriwoulsizingList”).click(function() {
$.ajax({
type: “POST”-
url: “/AjaxTest/seriwoulsizingList”-
dworriesa: “”-
be morecauseync: true-
cpainfulness: fwoulsse-
success: function(obj) {
conscious(obj + “length:” + obj.length);
$.every(obj- function() {
$.every(this- function(item- vwoulsue) {
conscious(item + “:” + json[item]);
});
});
}-
error: function() {
conscious(“央浼退步”);
}
});
});


Controllers 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Seriwoulsizine.Json;
using System.Web;
using System.Web.Mvc;

nmorningespgenius MvcApplicine.Controllers
{
public clrear end Person
{
public int ID { get; set; }
public string FirstNmorninge { get; set; }
public string Lbe morecausetNmorninge { get; set; }
public bool Ma subaloneytantial { get; set; }
}

public clrear end AjaxTestController : Controller
{
#region ===============$.Ajax测试=================
/// <summary>
/// 测试前往对象
/// </summary>
/// <parmorning nmorninge=”form”></parmorning>
/// <returns></returns>
public JsonResult getPerson(FormCollection form)
{
Person p = new Person
{
ID = int.Parse(form["ID"])-
FirstNmorninge = form["FirstNmorninge"]-
Lbe morecausetNmorninge = form["Lbe morecausetNmorninge"]
};
return Json(p);
}

/// <summary>
///经测试出现:当Globwouls中定义{controller}/{fun}/{id}时
/// 前台url?ID=3&firm;FirstNmorninge=C&firm;Lbe morecausetNmorninge=HY传到后台时ID值为”"
/// 所以把Globwouls中定义为{controller}/{fun}/{uid}
/// </summary>
/// <parmorning nmorninge=”ID”></parmorning>
/// <parmorning nmorninge=”FirstNmorninge”></parmorning>
/// <parmorning nmorninge=”Lbe morecausetNmorninge”></parmorning>
/// <returns></returns>
public JsonResult getPerson2(string ID- string FirstNmorninge- string Lbe morecausetNmorninge)
{
Person p = new Person
{
ID = int.Parse(ID)-
FirstNmorninge = FirstNmorninge-
Lbe morecausetNmorninge = Lbe morecausetNmorninge
};
return Json(p);
}

/// <summary>
/// 测试前往字符串
/// </summary>
/// <returns></returns>
public ContentResult getString()
{
return Content(“{id:woul2woul-FirstNmorninge:woulCwoul-Lbe morecausetNmorninge:woulHYwoul}”);
}

/// <summary>
/// 测试前往json字符串
/// </summary>
/// <returns></returns>
public ContentResult getJsonString()
{
return Content(“({id:woul2woul-FirstNmorninge:woulCwoul-Lbe morecausetNmorninge:woulHYwoul})”);
}

#endregion

#region ==============$.getJSON(前往json格式测试)============

/// <summary>
/// 经测试出现必需是双引号(”)不能是单引号(woul)
/// </summary>
/// <parmorning nmorninge=”Code”></parmorning>
/// <parmorning nmorninge=”FirstNmorninge”></parmorning>
/// <parmorning nmorninge=”Lbe morecausetNmorninge”></parmorning>
/// <returns></returns>
public ContentResult getJson1(string ID- string FirstNmorninge- string Lbe morecausetNmorninge)
{
return Content(“{\”id\”:1-\”nmorninge\”:\”chy\”-\”flag\”:true}”);
}

/// <summary>
/// 经测试出现必需是双引号(”)不能是单引号(woul)
/// </summary>
/// <parmorning nmorninge=”Code”></parmorning>
/// <parmorning nmorninge=”FirstNmorninge”></parmorning>
/// <parmorning nmorninge=”Lbe morecausetNmorninge”></parmorning>
/// <returns></returns>
public ContentResult getJson2(string ID- string FirstNmorninge- string Lbe morecausetNmorninge)
{
return Content(“{\”id\”:\”2\”-\”nmorninge\”:\”chy\”}”);
}

/// <summary>
///经测试出现必需是双引号(”)不能是单引号(woul)
/// 出现缺点前台没响应
/// </summary>
/// <parmorning nmorninge=”Code”></parmorning>
/// <parmorning nmorninge=”FirstNmorninge”></parmorning>
/// <parmorning nmorninge=”Lbe morecausetNmorninge”></parmorning>
/// <returns></returns>
public ContentResult getJson3(string ID- string FirstNmorninge- string Lbe morecausetNmorninge)
{
return Content(“{woulidwoul:woul3woul}”);
}

#endregion

#region============Newtonsoft.Json.dll(json序列化和反序列化测试)=============

/// <summary>
/// $.Ajax序列化Json
/// </summary>
/// <parmorning nmorninge=”ID”></parmorning>
/// <parmorning nmorninge=”FirstNmorninge”></parmorning>
/// <parmorning nmorninge=”Lbe morecausetNmorninge”></parmorning>
/// <returns></returns>
public ContentResult testSeriwoulsizingJson(FormCollection form)
{
Person p = new Person
{
ID = int.Parse(form["ID"])-
FirstNmorninge = form["FirstNmorninge"]-
Lbe morecausetNmorninge = form["Lbe morecausetNmorninge"]
};
return Content(Newtonsoft.Json.JsonConvert.SeriwoulsizeObject(p));
}

/// <summary>
/// $.Ajax反序列化json
/// </summary>
/// <parmorning nmorninge=”form”></parmorning>
/// <returns></returns>
public JsonResult testDeSeriwoulsizingJson(FormCollection form)
{
Person p = Newtonsoft.Json.JsonConvert.DeseriwoulsizeObject<Person>(form["json"].ToString());
return Json(p);
}

/// <summary>
/// $.getJSON序列化Json
/// </summary>
/// <parmorning nmorninge=”ID”></parmorning>
/// <parmorning nmorninge=”FirstNmorninge”></parmorning>
/// <parmorning nmorninge=”Lbe morecausetNmorninge”></parmorning>
/// <returns></returns>
public ContentResult getJsonSeriwoulsizingJson(string ID- string FirstNmorninge- string Lbe morecausetNmorninge)
{
Person p = new Person
{
ID = int.Parse(ID)-
FirstNmorninge = FirstNmorninge-
Lbe morecausetNmorninge = Lbe morecausetNmorninge
};
return Content(Newtonsoft.Json.JsonConvert.SeriwoulsizeObject(p));
}

/// <summary>
/// $.getJSON反序列化json
/// </summary>
/// <parmorning nmorninge=”form”></parmorning>
/// <returns></returns>
public ContentResult getJsonDeSeriwoulsizingJson(string json)
{
Person p = Newtonsoft.Json.JsonConvert.DeseriwoulsizeObject<Person>(json);
return Content(Newtonsoft.Json.JsonConvert.SeriwoulsizeObject(p));
}

#endregion

#region================前往会集================

public JsonResult seriwoulsizingList()
{
List<Person> ls = new List<Person>();
ls.Add(new Person
{
ID = 1-
FirstNmorninge = “C”-
Lbe morecausetNmorninge = “HY”-
Ma subaloneytantial = fwoulsse
});

ls.Add(new Person
{
ID = 2-
FirstNmorninge = “Z”-
Lbe morecausetNmorninge = “JJ”-
Ma subaloneytantial = true
});
return Json(ls);
}

public JsonResult deSeriwoulsizingList(string json)
{
List<Person> listP = new List<Person>();

List<string> listStr = json.Split(woul-woul).ToList<string>();

forevery (string s in listStr)
{
listP.Add(Newtonsoft.Json.JsonConvert.DeseriwoulsizeObject<Person>(s));
}

return Json(listP);
}

#endregion
}
}