Ajax请求小结

it2022-06-24  81

参数说明

ajax请求异步刷新页面=把需要异步刷新的页面单独写成一个.cshtml进行操作$.ajax({}); -------ajax方法。type: ------- 类型,此处为“POST” 还有 "GET"类型。必须全部大写。View和Controller请求方式保持一致。url: ------- 调用的Action 书写格式 /controller/actioncontroller为文件夹的名字,即不加后缀"controller"data: ------- 参数,没有可以不写data这一项success: function (sesponseTest) {} ------- 回调函数,就是当我的Action 执行完后,执行的方法。sesponseTest为Action返回的内容。$("#txt1").val(sesponseTest); ------- 把返回的字符串赋值给文本框。

无参数GET

$.ajax({ type: "GET", url: "/Js/GetWithoutParameter", data: { }, success: function () { layer.alert("Success!"); } }); 必须指定为JsonRequestBehavior.AllowGet ```C# public ActionResult GetWithoutParameter() { return Json("",JsonRequestBehavior.AllowGet); }

有参数GET

$.ajax({ type: "GET", url: "/Js/GetWithParameter", data: { "FirstName": "Liu", "LastName":"QingYu" }, success: function () { layer.alert("Success"); } }); public ActionResult GetWithParameter(string FirstName,string LastName) { return Json("",JsonRequestBehavior.AllowGet); }

多参数POST与返回值处理

$.ajax({ type: "POST", url: "/Js/PostWithParameters", data: { "number1": 1, "number2":2 }, success: function (result) { layer.alert("number1 + number2 = " + result); } }); [HttpPost] public ActionResult PostWithParameters(int number1,int number2) { return Json(number1+ number2); }

小结

(1)Ajax可以是POST/GET(2)传参方式第一种“key”:value, “key”:valuedata:{”index”:index,”name”:name….}第二种key:value, key:valuedata:{index:index,name:name….}第三种“key=”+value+”&&”+data:”index=”+index+”&&”+”name=”+name…(3)取值a.方法的参数列表

Public ActionResult GetData(int index,string name){…}

b.Request[“name”]

public ActionResult GetData{int index=Request[“Index”];….}

c.类对象

public ActionResult AddNews(userModel user) { string a=user.text1; string b=user.text2; }

d.从MVC封装的FormCollection容器中读取

public ActionResult AddNews(FormCollection form) { string a=form["text1"]; string b=form["text2"]; }

(4)其他 get方法window.open("/Js/PostWithParameters?number1="+ 1+ "&number2=" +2);

转载于:https://www.cnblogs.com/Lulus/p/7873831.html

相关资源:Jquery操作Ajax方法小结

最新回复(0)