我有一个MVC Spring控制器。我在页面加载时将其称为ajax。
$.ajax({
type: "get",
url: 'custom/topic',
data: "email1=" + email1 + "&email2=" + email2 ,
dataType: "json",
async: false,
success: function() {
alert("successfull!");
},
error: function (xhr, desc, error) {
window.alert('description' + desc);
window.alert('error' + error);
}
});
我的控制器是:
@RequestMapping(value="/topic", method=RequestMethod.GET)
public String topic(
@RequestParam("x1") String x1,
@RequestParam("x2") String x2) {
String result = custom.topic(x1, x2);
return "json";
网址是正确的,因为我没有找到消息。我的问题是它总是说解析器错误和无效的jason,并且在无效的jason前面显示整个页面代码。
答案 0 :(得分:1)
这是因为您在ajax调用中设置了dataType: "json",
并且没有将数据作为json数据发送。
转换此数据时Spring框架将失败,因为这不是json数据。
你可以做两件事(都是独立的),如下所示:
1. Remove ` dataType: "json",` from ajax call.
如果您真的想要使用JSON数据,请执行以下操作
2. pass the JSON data from client side and create a POJO mappedto JSON on
server side. example
var data = {};
data['email1'] = "email1";
data['email2'] = "email2";
JSON.stringify(data)
$.ajax({
type:"GET",
contentType: "application/json",
url: "custom/topic",
data: JSON.stringify(data),
POJO on server side will have:
two fields email1 and email2
答案 1 :(得分:0)
扩展到上述答案 [tag:'MVC controller - not working']
Parameters passed in Ajax call are email1 and email2,
but you are trying to retrieve x1 and x2.
答案 2 :(得分:0)
这是我的家庭控制器链接到动作按钮,应该运作良好。
namespace CampRoll.Controllers
{
public class HomeController : Controller
{
CampDb db = new CampDb(); // our database conn
//
// GET: /Home/
public ActionResult Index(string sortOrder)
{
ViewBag.PageTitle = "List of Camps (Total " + db.Childs.Count() + " Attending)";
if (sortOrder == null) sortOrder = "ascNumber";
ViewBag.numberOrder = (sortOrder == "ascNumber") ? "descNumber" : "ascNumber";
ViewBag.dateOrder = (sortOrder == "ascDate") ? "descDate" : "ascDate";
IQueryable<Camp> camps = db.Camps;
switch (sortOrder)
{
case "descDate":
ViewBag.dateOrder = "ascDate";
camps = camps.OrderByDescending(c => c.StartDate).Include("Children");
break;
case "descNumber":
ViewBag.numberOrder = "ascNumber";
camps = camps.OrderByDescending(c => c.Children.Count).Include("Children");
break;
case "ascDate":
ViewBag.dateOrder = "descDate";
camps = camps.OrderBy(c => c.StartDate).Include(c => c.Children);
break;
case "ascNumber":
ViewBag.numberOrder = "descNumber";
camps = camps.OrderBy(c => c.Children.Count).Include("Children");
break;
default:
ViewBag.numberOrder = "ascNumber";
camps = camps.OrderBy(c => c.Children.Count).Include("Children");
break;
}
return View(camps.ToList());
}
//
// GET: /Home/Details/5
public ActionResult Details(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var q = db.Camps.Find(id); // if no worky, q == null
if (q == null) // find record?
{
Debug.WriteLine("Record not found");
ViewBag.PageTitle = String.Format("Sorry, record {0} not found.", id);
//return new HttpStatusCodeResult(HttpStatusCode.NotFound);
}
else
{
ViewBag.PageTitle = "Details of " + q.Title + " (" + ((q.Children.Count == 0) ? "None" : q.Children.Count.ToString()) + ')';
ViewBag.SexStatsMale = q.Children.Count(chld => chld.Sex == Sex.Male);
ViewBag.SexStatsFemale = q.Children.Count(chld => chld.Sex == Sex.Female);
}
return View(q);
}
// Return Partial View of Leader Details
public PartialViewResult LeaderById(int id)
{
return PartialView("_Leader", db.Leaders.Find(id));
}
// Return Partial View of Children in a particular Camp
public PartialViewResult ChildrenById(int id)
{
var camp = db.Camps.Find(id);
@ViewBag.campId = id;
@ViewBag.campName = camp.Title;
return PartialView("_ChildrenInCamp", camp.Children);
}
//
// GET: /Home/Create
[HttpGet]
public ActionResult Create()
{
ViewBag.leadersList = db.Leaders.ToList();
return View();
}
//
// POST: /Home/Create
[HttpPost]
public ActionResult Create(Camp incomingCamp)
{
try
{
if (ModelState.IsValid)
{
using (var db = new CampDb())
{
db.Camps.Add(incomingCamp);
db.SaveChanges();
}
return RedirectToAction("Index");
}
return View();
}
catch
{
return View();
}
}
//
// POST: /Home/CreateChild
[HttpPost]
public PartialViewResult CreateChild(Child incomingChild)
{
if (ModelState.IsValid)
{
db.Childs.Add(incomingChild);
db.SaveChanges();
return ChildrenById(incomingChild.CampId);
}
return null;
}
//
// GET: /Home/Edit/5
public ActionResult Edit(int id)
{
ViewBag.leadersList = db.Leaders.ToList();
return View(db.Camps.Find(id));
}
//
// POST: /Home/Edit/5
[HttpPost]
public ActionResult Edit(Camp editCamp)
{
try
{
// Modifying State of an entity is more efficient
// as it avoids a remote call to the database
db.Entry(editCamp).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// POST: /Home/UpdateChild/5
[HttpPost]
public HttpStatusCodeResult UpdateChild(Child child)
{
try
{
db.Entry(child).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
catch
{
return new HttpStatusCodeResult(HttpStatusCode.NotFound);
}
}
//
// GET: /Home/Delete/5
public ActionResult Delete(int id)
{
return View(db.Camps.Find(id));
}
//
// POST: /Home/Delete/5
[HttpPost, ActionName("Delete")]
// This action named 'DeleteConfirmed' to present unique signature
public ActionResult DeleteConfirmed(int id)
{
db.Camps.Remove(db.Camps.Find(id));
db.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost, ActionName("DeleteChild")]
// This action named 'DeleteConfirmed' to present unique signature
public PartialViewResult DeleteChild(int id, int campId)
{
db.Childs.Remove(db.Childs.Find(id));
db.SaveChanges();
return PartialView("_ChildrenInCamp", db.Camps.Find(campId).Children);
}
}
}