Home  >  Article  >  Web Front-end  >  JQuery submits forms via Ajax and returns results_jquery

JQuery submits forms via Ajax and returns results_jquery

WBOY
WBOYOriginal
2016-05-16 18:04:121261browse

as follows:

1: Non-Ajax

Front desk:

image

Corresponding background:

image

2: JQuery Ajax

Before introducing the use of JQuery to submit forms, we need to first reference jquery.form.js, which comes from http://www.malsup.com/jquery/form/. At this point, the JS files we need to reference are:

image

Functional requirements: Ajax submits the form, processes the request in the controller HelloWorld4Controller, and returns some data. The data format is JSON.

First, we assume that the returned JSON entity is:

Copy code The code is as follows:

public class LoginResultDTO
{
public bool Success { get; set; }
public string Message { get; set; }
public string ReturnUrl { get; set; }
}

The code of the controller part is:
Copy the code The code is as follows:

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();
}
}
}

The above code tells us that it is very convenient if we want to return other entities.

Frontend part code:

image

If the button does not use submit, but button, then the code in the above picture should be changed to the following form:

image
Source code download: MvcApplication5.zip

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn