How to make an ajax call that reverses a string ?

How to make an ajax call that reverses a string ?

How to make an ajax call that reverses a string ?  

it does not do the ajax call, but goes straight into the controller reversethestring...
            i am getting a  null reference exception ...string array is empty */

            $.ajax({
                type: "GET",
                url: './ReverseTheString',
                data: { input: 'abc'},
                dataType: "json",
                async: true,
                success: function (result) {
                    $("#result2").html(result);
                }
            });



In home controller i have  :


        public string ReverseTheString(string input)
        {
            char[] arr = input.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);
        }


Index.cshtml:


  1. @{
  2.     Layout = null;
  3. }



  4. <!-- -->

  5. <!DOCTYPE html>
  6. <html>
  7. <head>

  8.     <script src="~/Scripts/jquery-1.10.2.js"></script>

  9.     <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

  10.     <script>

  11.         $(document).ready(function () {
  12.             $("#button1").click(function () {

  13.                 $.ajax({

  14.                     type: 'GET',
  15.                    
  16.                     url: '@Url.Action("HelloAjax" , "Home")',
  17.                     success: function (result) {

  18.                         $("#div1").html(result);

  19.                     }

  20.                 });  // closes the ajax


  21.             });  // closes click function



  22.             
  23.             $("#button2").click(function () {

  24.                 $.ajax({

  25.                     type: 'GET',
  26.                     data: { a: 1, b: 2 },
  27.                     url: '@Url.Action("Sum" , "Home")',
  28.                     success: function (result) {

  29.                         $("#div3").html(result);

  30.                     }

  31.                 });  // closes the ajax


  32.             });  // closes click function














  33.             // data: is the data returned by the controller

  34.             var url = "/Home/ReverseTheString";
  35.             var stringToReverse = "Bob Cravens";
  36.             $.get(url, { input: stringToReverse }, function (data) {
  37.                 $("#div4").html(data);
  38.             });




  39.             $(".sum").click(function () {

  40.                 $.ajax({

  41.                     type: 'GET',
  42.                     data: { a:1 , b:2} , 
  43.                     url: '@Url.Action("Sum" , "Home")',
  44.                     success: function (result) {

  45.                         $("#div3").html(result);

  46.                     }

  47.                 });  // closes the ajax


  48.             });  // closes click function

  49.         
  50.              

  51.             /* this part does not work , it does not do the ajax call, but goes straight into the controller reversethestring...
  52.             null reference exception ...string array is empty */

  53.             $.ajax({
  54.                 type: "GET",
  55.                 url: './ReverseTheString',
  56.                 data: { input: 'abc'},
  57.                 dataType: "json",
  58.                 async: true,
  59.                 success: function (result) {
  60.                     $("#result2").html(result);
  61.                 }
  62.             });




  63.             // . IS ALREADY THE HOME FOLDER... /HOME/HOME

  64.             var formData = { name: "ravi", age: "31" }; //Array 

  65.             $.ajax({
  66.                 url: "./Index ",
  67.                 type: "POST",
  68.                 data: formData,
  69.                 success: function (data, textStatus, jqXHR) {

  70.                     $("#div3").html("abbb");

  71.                     //data - response from server
  72.                 },
  73.                 error: function (jqXHR, textStatus, errorThrown) {

  74.                 }
  75.             });





  76.             $.post('superman', { field1: "hello", field2: "hello2" },
  77.     function (returnedData) {
  78.         console.log(returnedData);
  79.     });
  80.    


  81.         });       // closes document ready function
  82.     </script>





  83.     <script>

  84.         var url = "/Home/GetDateTimeString";
  85.         $.get(url, null, function (data) {
  86.             $("#div2").html(data);
  87.         });




  88.         var url = "/Home/sum";
  89.         $.get(url, null, function (data) {
  90.             $("#div7").html(data);
  91.         });




  92.         var url = "/Home/RegisterUser";
  93.         var first = "Bob";
  94.         var last = "Cravens";
  95.         $.post(url, { firstName: first, lastName: last }, function (data) {
  96.             $("#div5").html(data);
  97.         });


  98.         var f = $("#myForm");
  99.         var url = f.attr("action");
  100.         var formData = f.serialize();
  101.         $.post(url, formData, function (data) {
  102.             $("#div6").html(data);
  103.         });




  104.         // works if div 7,div8,div9 are changed to div4,div5,div6 

  105.         var url = "/Home/GetContactInfo";
  106.         var id = 123;
  107.         $.getJSON(url, { personId: id }, function (data) {
  108.             $("#div7").html(data.FirstName + " " + data.LastName);
  109.             $("#div8").html(data.State);
  110.             $("#div9").html(data.Age);
  111.         });



  112.         var url = "/Home/MyPartialView";
  113.         var info = "This is my information."

  114.         

  115.         // note : data is the return data from the controller , we have to specify it here as a parameter for function
  116.         $.get(url, null, function (data) {
  117.             $("#div1").html(info);
  118.         });


  119.     </script>



  120.         <title>A study of population dynamics</title>


  121.     </head>
  122.     <body>


  123.         <div class="container">
  124.             <form id="myForm" action="/Home/FormPost" method="post">


  125.                 <div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

  126.                 <div id="div2"><h2>Let jQuery AJAX Change This Text</h2></div>

  127.                 <div id="div3"><h2>Let jQuery AJAX Change This Text</h2></div>

  128.                 <div id="div4"><h2>Let jQuery AJAX Change This Text</h2></div>


  129.                 <div id="div5"><h2>Let jQuery AJAX Change This Text</h2></div>

  130.                 <div id="div6"><h2>Let jQuery AJAX Change This Text</h2></div>

  131.                 <div id="div7"><h2>Let jQuery AJAX Change This Text</h2></div>

  132.                 <div id="div8"><h2>Let jQuery AJAX Change This Text</h2></div>

  133.                 <div id="div9"><h2>Let jQuery AJAX Change This Text</h2></div>

  134.                 <a  href="#" class= "sum"> Sum </a>

  135.                 <div id="result2"><h2>  "result2 "</h2></div>

  136.                 <div>First Name: <input name="FirstName" type="text" value="Bob" /></div>
  137.                 <div>Last Name: <input name="LastName" type="text" value="Cravens" /></div>
  138.                 <div>Age: <input name="Age" type="text" value="43" /></div>
  139.                 <input type="submit" value="Save Contact" />
  140.                 <div id="postResult">?</div>
  141.             </form>


  142.         </div>

  143.             <button  id="button1">Get External Content</button>


  144.             <button  id="button2"  >Get External Content</button>












  145. </body>
  146.     </html>