2

I have an action link which I want to do an HTTP Post to my Controller, however, I keep getting a HTTP 500.

Here is my jQuery

<script type="text/javascript">

    $(document).ready(function () {
        $('.thing').click(function (e) {
            e.preventDefault();
            $.ajax({
                url: this.href,
                dataType: "json",
                type: "POST",
                success: function (data) {
                    if (data.Success == true) {
                       // do something
                    }
                    else {
                        alert(data.Message);
                    }
                },
                error: function (textStatus, errorThrown) {
                    // request always errors
                }
            });
        });
    });    
</script>

and my Action link code

@Html.ActionLink("my link", "DoStuff", "Dude", new { thingId = item.Id }, new { @class = "thing" })

My Controller

public class DudeController : Controller
{
    [HttpPost]
    public ActionResult DoStuff(int thingId)
    {
        return Json(new { Success = true, Message = string.Empty }, JsonRequestBehavior.AllowGet);
    }

}

Any ideas?

5
  • your controller is expecting an integer value and you are sending none in your ajax call?? Commented Apr 17, 2011 at 19:45
  • @3nigma The @Html.ActionLink code generates the url /somewhere/Dude/DoStuff/2 where 2 is my thingId integer in my controller ... I'm trying to just send a http post to that url. Am I misunderstanding something here? Commented Apr 17, 2011 at 19:54
  • o yup!! i didnt see it earlier sry... Commented Apr 17, 2011 at 19:56
  • 2
    i would first use fiddler and to see what the request is. I would then remove the HttpPost from DoStuff method and try accessing the url the ActionLink produces. Commented Apr 17, 2011 at 20:13
  • @Circadian - Post looks good in Fiddler, found the problem though. Was a crappy job on my part, had an exception in one of the controller's dependencies. Commented Apr 17, 2011 at 20:37

2 Answers 2

0

Can you try to remove dateType from ajax call and add data? For me it works:

$(document).ready(function () {
$('.thing').click(function (e) {
    e.preventDefault();
    $.ajax({
        url: this.href,
        type: "POST",
        data: this.thingId,
        success: function (data) {
            if (data.Success == true) {
                // do something
                alert(data.Message);
            }
            else {
                alert(data.Message);
            }
        },
        error: function (textStatus, errorThrown) {
            // request always errors
        }
    });
});
});
Sign up to request clarification or add additional context in comments.

Comments

0

For anyone that encounters this issue, here's another potential point of interest. In my controller I had the following:

    var events = (from e in db.EventMsts                                                      
    where e.Eventid.Equals(eventID)
    orderby e.EventNm
    select new { e.Eventid, e.EventNm, e.EventDescription, e.URLEventSite, e.URLTicketSales,e.EventDates  })
                                    .Take(1)
                                    .ToArray();
    return Json(events);

Which was not throwing any exception, and then the browser would receive a HTTP 500.

e.EventDates was a collection of objects while the others were strings/integers. Simply removing e.eventDates from the controller (and the ajax request) got everything to be successful (aside from missing the data I want).

Per Craig's response there are security limitations that prevents passing back arguments like that (Craig I'm just trusting you on this since I've not actually seen a doc regarding this). The solution was to tear my EventDates object down to a single delimited string so that I can parse it in jQuery. Here's what my code became and now works for me - note an event has event Dates, and an event date has event times.

    var dateTimeList = new StringBuilder();

    var times = (from t in db.EventTimes
          where t.EventDate.Eventid.Equals(eventID)
                             orderby t.EventStartTm
                             select
                                new {
                                        t.EventDate.EventDt,
                                        t.EventStartTm,
                                        t.EventEndTm}).ToArray();
    foreach (var time in times)
    {
        if (dateTimeList.Length > 0)
        {
            dateTimeList.Append("|" + time.EventDt + " from " + time.EventStartTm + " to " + time.EventEndTm ?? "whenever");
        }
        else
        {
            dateTimeList.Append(time.EventDt + " from " + time.EventStartTm + " to " + time.EventEndTm ?? "whenever");
        }
    }
    var dateTimeString = dateTimeList.ToString();
    var evnt = (from e in db.EventMsts
                where e.Eventid.Equals(eventID)
                orderby e.EventNm
        select new { e.Eventid, e.EventNm, e.EventDescription, e.URLEventSite, e.URLTicketSales, dateTimeString })
        .FirstOrDefault();

    return Json(evnt);

3 Comments

You need to add JsonRequestBehavior.AllowGet. This is a security feature of .NET so when returning Json result from a GET you must explicitly state AllowGet
@Adam and @Carl: The security feature is precisely so that you don't return an array as a top-level JSON response. Since that's what Carl is doing here, the best solution is to wrap up the array in an object before specifying AllowGet
Craig - that's exactly what I just got done doing - I return my eventDates as a delimited string that I can parse easily in my jQuery. It's a little ugly in the controller, but I understand the limit now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.