MVC控制器-不起作用

I have a MVC Spring controller. I call this ajax on page load.

$.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);
        }
    });

My controller is:

@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";

The url is correct because I didn't get not found message. My issue is that it always says parser error and Invalid jason and in front of invalid jason it shows the whole page code.

This is because you have set the dataType: "json", in your ajax call and are not sending the data as json data.

The Spring framework will fails while converting this data as this is not a json data.

You can do two things (both independent) as below:

1. Remove `  dataType:   "json",` from ajax call.

If your really want to consume JSON data then do the following

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

Extending to the above answer [tag:'MVC controller - not working']

   Parameters passed in Ajax call are email1 and email2, 
   but you are trying to retrieve x1 and x2.

This is my home controller linked to action buttons, should work well.

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

}