简体   繁体   中英

JQuery not calling controller in asp.net mvc

I am trying to make a call from views to controller using jquery. however it never call controller.

I have a Umbraco 7.1.2 website in asp.net MVC 4 application.

My JS

var dp = jQuery;
dp.noConflict();
dp(document).ready(function() {
    GetTestimonials();
});

function GetTestimonials() {
   dp.ajax({
        type: 'GET',
        url: '/Home/GetTestimonialsList',
        cache: false,
        success: function (data) {
            dp.each(data, function (index, val) {
                alert(val.Name + val.Comment + val.Date);
            });
        },
        error: function () {
            alert("error");
        }
    });
}

Then my controller

[HttpGet]
public JsonResult GetTestimonialsList()
{
     var model = _spdb.TestimonialDetails.Where(t => t.Status == Enums.TestimonialsStatus.approved).Select(t => new { t.Comment, t.Name, t.Date});
     return Json(model, JsonRequestBehavior.AllowGet);
}

I put a breakpoint on the controller, it never got called.

On the browser, no errors were raised with jquery.

However it always triggers alert("error"); when page loads.

I called console.log("test"); it displays on the browser debug.

On same page I am making a form post to controller using jquery and it has no problem at all.

the query from db gives me the data right.

What am I doing wrong?

Thanks a lot

Ok, found the solution. I changed the URL to url: '/umbraco/surface/home/GetTestimonialsList' , and works fine.

Try this. You are returning IQuerable.

[HttpGet]
public JsonResult GetTestimonialsList()
{
     var model = _spdb.TestimonialDetails.Where(t => t.Status == Enums.TestimonialsStatus.approved).Select(t => new { t.Comment, t.Name, t.Date}).ToList();
     return Json(model, JsonRequestBehavior.AllowGet);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM