Overview: Here in this article will see asp.net jQuery Ajax JSON example, i.e. calling jQuery Ajax WebMethod and get JSON format result (JSON data) in Asp.net C# Webform. In short a simple jQuery Ajax JSON example in Asp.net C# with MS SQL Server database.If you are looking for how to get JSON formatted result from jquery ajax call in asp.net C#, then this post will explain step by step process. You can also check 3 way to convert DataTable into JSON String in Asp.net C#.Bạn đang xem: Jquery datatables with ajax

What is jQuery AJAX?

AJAX stands for Asynchronous JavaScript and XML and with using ajax we load data from the server without reloading the whole web page refresh. i.e we can update a specific part of our web page dynamically without page refresh. By using jQuery AJAX methods, we can request HTML, XML, or JSON from the server using both HTTP Get and HTTP Post request.

Đang xem: Top 19 jquery datatable load data ajax example mới nhất 2022

Am assuming you are aware of Asp.net C# Webservice, i.e., (ASMX file) Webservice.ASMX, WebMethods and familiar with jQuery Ajax syntax.

And it’s very useful if you want to save or insert data into the database without page postback or page refresh .i.e Insert data into database using jQuery Ajax.

In this tutorial, we make a jQuery Ajax call and in response will get the JSON data, i.e., here we get a list of all cars and other detail information from our database ( MS SQL server) via WebMethod (ASMX file).

Steps to create jQuery Ajax JSON example in Asp.net C#.

Download the jQuery library and add Html Markup.Calling jQuery Ajax method.C#: Create Class and WebMethod, which returns JSON Object.

Fig 1:

*

Here in our case, we fetch the list of cars along with other information from the database MS SQL server via C# WebMethod, which we create later on server-side.

Let’s first check how our Client side code looks as shown below

//*$(“#myButton”).on(“click”, function (e) { e.preventDefault(); var aData= ; aData = $(“#ddlSelectYear”).val(); $(“#contentHolder”).empty(); var jsonData = JSON.stringify({ aData:aData}); $.ajax({ type: “POST”, //getListOfCars is my webmethod url: “WebService.asmx/getListOfCars”, data: jsonData, contentType: “application/json; charset=utf-8”, dataType: “json”, // dataType is json format success: OnSuccess, error: OnErrorCall }); function OnSuccess(response) { console.log(response.d) } function OnErrorCall(response) { console.log(error); } });//*Here in above code on our button click, we make an ajax call.

You may notice we have set dataType=”json” that is because we want our ajax response in JSON format.

Xem thêm: 20+ Free Transactional Html Email Template S 2022, Invoice Email Template

# Creating a class and WebMethod, which returns list collection as JSON data.

Now we add a Webservice file (ASMX file) in our project, here we added a file and named as myfunction.asmx. In our Webservice (.asmx file) we also created a class .i.e Cars which has property as carName, carRating, carYear.

Now we write a WebMethod as we want to get the list of all cars, here getListofCars is our WebMethod, which fire the select query to the database, pull the record from the database and add it to our class object Car.

In response, we return this Car class object.

//* //Created a class public class Cars { public string carName; public string carRating; public string carYear; }//*Now we write our WebMethod, which returns List of Cars details.

//*public ListCars> getListOfCars(Liststring> aData){ SqlDataReader dr; ListCars> carList = new ListCars>(); using (SqlConnection con = new SqlConnection(conn)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = “spGetCars”; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = con; cmd.Parameters.AddWithValue(“makeYear”, aData); con.Open(); dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (dr.HasRows) { while (dr.Read()) { string carname = dr.ToString(); string carrating = dr.ToString(); string makingyear = dr.ToString(); carList.Add(new Cars { carName = carname, carRating = carrating, carYear = makingyear }); } } } } return carList;}//*In response, we will get data in JSON format, check this in your browser console, JSON format list of cars is available as shown above in Fig 1.

function OnSuccess(response.d)) { console.log(response.d)}Note: Always use response.d to get the jquery ajax response in Asp.net C#.

Now we write some more code on our client-side .i.e how to display JSON data list of the car into our Web page.

In short, we have to write some jQuery code to handle the response `JSON Data`. So now our success function looks like as shown below.

Xem thêm: Những Công Hiệu Của Thuốc Giảm Co Thắt Tử Cung Spasmaverine (Alverin)

//*function OnSuccess(response) { var items = response.d; var fragment=”” $.each(items, function (index, val) { var carName = val.carName; var carRating = val.carRating; var carYear = val.carYear; fragment += ” “+carName+” :: “+carRating+” – “+carYear+””; }); $(“#contentHolder”).append(fragment);}//*Here we stored the response in variable item and then using the jQuery $.each() method we make a loop over data, get and set the value to ‘’ tag and append to parent “

# OutPut: Asp.net ajax JSON example:

Finally, our Output which displays the list of car and other information.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *