0

Why does the "action" input parameter not recognized by the ajax call? My code looks like this:

<script type="text/javascript">
                $(document).ready(function (string action) {
                    $.ajax({
                        type: "GET",
                        url: @Url.Action(action, "Default") ,
                        data: param = "",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: successFunc,
                        error: errorFunc
                    }).done(function () {
                        alert("Success");
                    }).error(function () {
                        alert("Faile");
                    });
                }
            </script>

enter image description here

Su Llewellyn
  • 2,660
  • 2
  • 19
  • 33
  • Most likely because serverside code is evaluated at render time... and not sure where you learned `(string action)` – epascarello Jun 12 '18 at 17:07
  • You can't pass parameters to $(document).ready(), please see this related post (https://stackoverflow.com/questions/5976938/how-to-pass-parameters-to-jquery-document-ready-function-asp-net-mvc-c) – Ryan Wilson Jun 12 '18 at 17:07
  • ah! Want to enter this as an answer, Ryan, so I can accept it? – Su Llewellyn Jun 12 '18 at 17:08
  • @SuLlewellyn Removed as the haters began hating. – Ryan Wilson Jun 12 '18 at 17:10
  • There's a whole lot of mixing languages happening in your code here. Specifying a `string` type in JavaScript, trying to use a JavaScript variable in C#... While it's talking about different languages, you'd really do well to read and understand the concepts presented in detail here: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – David Jun 12 '18 at 17:11
  • Since mplungjan found this question to be a duplicate, I can't accept Ryan's answer. My apologies to Ryan – Su Llewellyn Jun 12 '18 at 17:15
  • @David - you are right - I ended up ripping out my code and starting over. The problem turned out to be one of Javascript scope -- I noticed that when I placed my ajax call inside the script associated with the command link the user would click, then my call would be activated. – Su Llewellyn Jun 20 '18 at 23:04

0 Answers0