2

I have a JavaScript function that triggers on a button click, but the Chrome Developer Tools skips over the AJAX Post. A 500 Error occurs, but break points inside the Controller are never hit, so none of the variables appears to be passing. I've seen many JavaScript questions on this site, but couldn't find one that addressed this situation. Here's what my code looks like and thanks in advance!

JavaScript:

$("#calculate").click(function () {
        var $indicator = $("#Indicator");
        $.ajax({
            type: "POST", //THIS IS NEVER HIT, JUST SKIPPED OVER!
            url: '@Url.Action("LogPrices", "Sales")',
            data: {
                indicator: $indicator.val(), iD: $("#ID").val()
            },
            success: function (data) {
            // logic
        }
    });
});

Controller:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogPrices(bool indicator, long iD)
{
    // logic
    return Json(priceLog, JsonRequestBehavior.AllowGet);
}
jle
  • 269
  • 8
  • 25
  • Is there any message with your 500 internal server error? – Peter Rasmussen Feb 01 '16 at 20:18
  • @Peter Rasmussen - Nothing useful as far as I can tell. It says "POST [my localhost]/Sales/LogPrices 500 (Internal Server Error)" – jle Feb 01 '16 at 20:22
  • Possible duplicate of [Url.Action in jquery ajax: Second parameter not passed](http://stackoverflow.com/questions/25367062/url-action-in-jquery-ajax-second-parameter-not-passed). Says in there to wrap the `Url.Action` in `Html.Raw` to prevent esacping, does that help? – Tyler StandishMan Feb 01 '16 at 20:26
  • First find out the details behind the 500 error. http://stackoverflow.com/questions/2640526/detailed-500-error-message-asp-iis-7-5 – Jasen Feb 01 '16 at 20:27
  • Thanks all - I will investigate those 2 links and report back. – jle Feb 01 '16 at 20:28

1 Answers1

2

Remove

[ValidateAntiForgeryToken]

this mechanism is used for some security measures, if you want to use it, read about it

bascially you have to send the token to the controller, other wise it's going to give an error because the security measure failed - no token given-

you can just add this

@Html.AntiForgeryToken()

and then submit it as a form, MVC will automatically take care of this and validates the token for you

EDIT: missing one of the parameters of your controller action might cause 500 as well, in your case the ID and the indicator should both be sent, make sure your html is retrieving the values correctly

usually I start tracking the error by making static values

Hassan Khallouf
  • 1,170
  • 1
  • 13
  • 30
  • I have tested this by commenting out the validation and the error still occurs. Also, the @Html.AntiForgeryToken() is added to the form, but this AJAX Post occurs outside the total form submission by design as a button instead of an input. I left the validation in the Controller to remind myself to add it to the AJAX call like this: http://stackoverflow.com/questions/4074199/jquery-ajax-calls-and-the-html-antiforgerytoken. – jle Feb 01 '16 at 20:32
  • sending this ajax without the form won't send the token, but you are saying it still gives the error even without it? I tried your exact code and got 500, just removed it and it worked, try to double check your controller name and action name, try placing static id and indicator, I forced (true,2) in my call and it worked perfectly – Hassan Khallouf Feb 01 '16 at 20:37
  • making either the ID or the indicator null will give 500 as well, make sure your html is getting them correctly, try putting constant values in the ajax and see if it's still failing , (keep the AntiForgeryToken commented until you make sure ), you might have more than one problem together – Hassan Khallouf Feb 01 '16 at 20:43
  • I just discovered that was the error at about the same time. A required field wasn't set as required on the form. Thanks and if you please write this as another answer and I'll mark it as correct! – jle Feb 01 '16 at 20:45
  • glad you solved it, I just added this to the same answer for future people reading this :) – Hassan Khallouf Feb 01 '16 at 20:48