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:
- @{
- Layout = null;
- }
- <!-- -->
- <!DOCTYPE html>
- <html>
- <head>
- <script src="~/Scripts/jquery-1.10.2.js"></script>
- <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
- <script>
- $(document).ready(function () {
- $("#button1").click(function () {
- $.ajax({
- type: 'GET',
-
- url: '@Url.Action("HelloAjax" , "Home")',
- success: function (result) {
- $("#div1").html(result);
- }
- }); // closes the ajax
- }); // closes click function
-
- $("#button2").click(function () {
- $.ajax({
- type: 'GET',
- data: { a: 1, b: 2 },
- url: '@Url.Action("Sum" , "Home")',
- success: function (result) {
- $("#div3").html(result);
- }
- }); // closes the ajax
- }); // closes click function
- // data: is the data returned by the controller
- var url = "/Home/ReverseTheString";
- var stringToReverse = "Bob Cravens";
- $.get(url, { input: stringToReverse }, function (data) {
- $("#div4").html(data);
- });
- $(".sum").click(function () {
- $.ajax({
- type: 'GET',
- data: { a:1 , b:2} ,
- url: '@Url.Action("Sum" , "Home")',
- success: function (result) {
- $("#div3").html(result);
- }
- }); // closes the ajax
- }); // closes click function
-
-
- /* this part does not work , it does not do the ajax call, but goes straight into the controller reversethestring...
- 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);
- }
- });
- // . IS ALREADY THE HOME FOLDER... /HOME/HOME
- var formData = { name: "ravi", age: "31" }; //Array
- $.ajax({
- url: "./Index ",
- type: "POST",
- data: formData,
- success: function (data, textStatus, jqXHR) {
- $("#div3").html("abbb");
- //data - response from server
- },
- error: function (jqXHR, textStatus, errorThrown) {
- }
- });
- $.post('superman', { field1: "hello", field2: "hello2" },
- function (returnedData) {
- console.log(returnedData);
- });
-
- }); // closes document ready function
- </script>
- <script>
- var url = "/Home/GetDateTimeString";
- $.get(url, null, function (data) {
- $("#div2").html(data);
- });
- var url = "/Home/sum";
- $.get(url, null, function (data) {
- $("#div7").html(data);
- });
- var url = "/Home/RegisterUser";
- var first = "Bob";
- var last = "Cravens";
- $.post(url, { firstName: first, lastName: last }, function (data) {
- $("#div5").html(data);
- });
- var f = $("#myForm");
- var url = f.attr("action");
- var formData = f.serialize();
- $.post(url, formData, function (data) {
- $("#div6").html(data);
- });
- // works if div 7,div8,div9 are changed to div4,div5,div6
- var url = "/Home/GetContactInfo";
- var id = 123;
- $.getJSON(url, { personId: id }, function (data) {
- $("#div7").html(data.FirstName + " " + data.LastName);
- $("#div8").html(data.State);
- $("#div9").html(data.Age);
- });
- var url = "/Home/MyPartialView";
- var info = "This is my information."
-
- // note : data is the return data from the controller , we have to specify it here as a parameter for function
- $.get(url, null, function (data) {
- $("#div1").html(info);
- });
- </script>
- <title>A study of population dynamics</title>
- </head>
- <body>
- <div class="container">
- <form id="myForm" action="/Home/FormPost" method="post">
- <div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
- <div id="div2"><h2>Let jQuery AJAX Change This Text</h2></div>
- <div id="div3"><h2>Let jQuery AJAX Change This Text</h2></div>
- <div id="div4"><h2>Let jQuery AJAX Change This Text</h2></div>
- <div id="div5"><h2>Let jQuery AJAX Change This Text</h2></div>
- <div id="div6"><h2>Let jQuery AJAX Change This Text</h2></div>
- <div id="div7"><h2>Let jQuery AJAX Change This Text</h2></div>
- <div id="div8"><h2>Let jQuery AJAX Change This Text</h2></div>
- <div id="div9"><h2>Let jQuery AJAX Change This Text</h2></div>
- <a href="#" class= "sum"> Sum </a>
- <div id="result2"><h2> "result2 "</h2></div>
- <div>First Name: <input name="FirstName" type="text" value="Bob" /></div>
- <div>Last Name: <input name="LastName" type="text" value="Cravens" /></div>
- <div>Age: <input name="Age" type="text" value="43" /></div>
- <input type="submit" value="Save Contact" />
- <div id="postResult">?</div>
- </form>
- </div>
- <button id="button1">Get External Content</button>
- <button id="button2" >Get External Content</button>
- </body>
- </html>