Reputation: 3013
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?
Upvotes: 2
Views: 2735
Reputation: 318
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);
Upvotes: 0
Reputation: 905
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
}
});
});
});
Upvotes: 0