联系我们
简单又实用的WordPress网站制作教学
当前位置:网站首页 > 网页前端技术 > 正文

JQuery通过Ajax提交表单并返回结果 - Web前端

作者:98wpeu发布时间:2026-07-22分类:网页前端技术浏览:8


导读:如下:1:非Ajax前台:对应后台:2:JQuery之ajax在介绍使用jQuery提交表单前,我们需要首先引用jquery.FORM.JS,它来自于http://www.mal...
如下:

1:非Ajax

前台:

image

对应后台:

image

2:JQuery之ajax

在介绍使用jQuery提交表单前,我们需要首先引用jquery.FORM.JS,它来自于http://www.malsup.com/jquery/form/,至此,我们需要引用的js文件有:

image

功能要求:Ajax提交表单,在控制器HelloWorld4controller中处理请求,并返回若干数据,数据格式为json。

首先,我们假设返回的JSON实体为:
复制代码 代码如下:
public class LoginResultDTO
{
public bool Success { get; set; }
public string Message { get; set; }
public Stringreturnurl { get; set; }
}

控制器部分的代码为:
复制代码 代码如下:
public class HelloWorld4Controller : Controller
{
public ActionResult Index()
{
if (Request.IsAjaxRequest())
{
string str1 = Request.Form["text33"];
string str2 = Request.Form["Text44"];
return Json(new MVCAPPlication5.Controllers.HelloWorld3Controller.LoginResultDTO { Success = true, Message = str1 + str2, ReturnUrl = "SomeUrl" });
}
else
{
return View();
}
}
}

上面的代码告诉我们,如果我们想要返回别的实体,也是很方便的的。

前台部分代码:

image

如果按钮不使用submit,而是button,则上图代码换成如下形式:

image
源码下载:MvcApplication5.zip

标签:表单WebAjaxJQuery


网页前端技术排行
最近发表
网站分类
标签列表