Introduction:

Here we are going to explain what is get() method  or how to use get() method or why use get() method  or how to send an HTTP GET  request to the server and retrieve data without browser postback/refresh  in jQuery with example.

Description: 

The jQuery get() method use to send an HTTP GET request to the server and retrieve the data without browser postback/refresh.  

  • Let’s see a demonstration example of get() method 

Index.cshtml:

 @{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
</head>
<body>
    <div>
        <input type="button" id="btnGet" value="Load Data using Get" />
        <div id="divResult"></div>
        <script>
            $("#btnGet").click(function () {
                $.get(
                "/Tutorials/GetJsonData",
                function (data) {
                    $("#divResult").html("<b>The result is:</b> " + JSON.stringify(data) +"");
                });
            });
        </script>
    </div>
</body>
</html>

 

TutorialsController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace Tutorials.Controllers
{
    public class TutorialsController : Controller
    {
        // GET:/Tutorials/
        public ActionResult Index()
        {
            return View();
        }
        public JsonResult GetJsonData()
        {
            Students[] obj = new Students[]  
            {
                new Students()  
                    {    
                       StudentId = 1, 
                       Name = "NitinTyagi"
                       Marks = 400 
                    }, 
                    new Students()  
                    { 
                        StudentId = 2, 
                        Name = "AshishTripathi"
                        Marks = 500 
                    } 
            };
            return Json(obj, JsonRequestBehavior.AllowGet);
        }
 
    }
    public class Students
    {
        public int StudentId { get; set; }
        public string Name { get; set; }
 
        public int Marks { get; set; }
 
    }
}
 

Here on click of “btnGet” button, get() method will fire.

Here

  1. url: Any valid url
  2. data : data to send to the server
  3. success: function to execute when the request is processed successfully, the “data” is the exact value that will have all the response string returned from the server.

 In above code when we click on button then send an http get request to the server and retrive data indivResult”.

Output:

  .get() - Get request to server in jQuery

I hope it will help to you.

 

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like