Calling asp net web services using jquery ajax

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Step 1 : Create SQL Server table and insert employee data

Step 2 : Create a stored procedure to retrieve employee data by ID

namespace Demo
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
}
}

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;

namespace Demo
{
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class EmployeeService : System.Web.Services.WebService
{
[WebMethod]
public Employee GetEmployeeById(int employeeId)
{
Employee employee = new Employee();

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetEmployeeById", con);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter parameter = new SqlParameter();
parameter.Value = employeeId;

cmd.Parameters.Add(parameter);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while(rdr.Read())
{
employee.ID = Convert.ToInt32(rdr["Id"]);
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.Salary = Convert.ToInt32(rdr["Salary"]);
}
}

return employee;
}
}
}

Step 6 : Add an HTML page to the ASP.NET project. Copy and paste the following HTML and jQuery code

<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
$('#btnGetEmployee').click(function () {

var empId = $('#txtId').val();

$.ajax({
data: { employeeId: empId },
method: 'post',
dataType: 'xml',
success: function (data) {
var jQueryXml = $(data);
},
error: function (err) {
alert(err);
}
});
});
});
</script>
</head>
<body style="font-family:Arial">
ID : <input id="txtId" type="text" style="width:100px" />
<input type="button" id="btnGetEmployee" value="Get Employee" />
<br /><br />
<table border="1" style="border-collapse:collapse">
<tr>
<td>Name</td>
<td><input id="txtName" type="text" /></td>
</tr>
<tr>
<td>Gender</td>
<td><input id="txtGender" type="text" /></td>
</tr>
<tr>
<td>Salary</td>
<td><input id="txtSalary" type="text" /></td>
</tr>
</table>
</body>
</html>

Рекомендации по теме
Комментарии
Автор

Many praises to you my friend! This is the first video that I've seen that clearly explains how to make an ajax call using ASP.NET. It is very easy to understand and explained in such a clear, friendly way. I like that you did it with straight ADO and POCO objects instead of going Entity Framework. Keep up the awsome work!

ssrafael
Автор

I wonder, how can people dislike such wonderful, crystal clear explanation which can be used even till date...., if there was option, i would have given thousands of likes. Keep up the good work Sir!!!!

sandeepraul
Автор

Thank You Very Much !!!
After lost many hours, you solved my problem in 15 minutes !!!
Excelent work !! - Thanks Again !!!

devve
Автор

Thank you, I was looking on how to do this for 6 hours straight, you saved my semester

armandocamacho
Автор

Thank you buddy! Was really stuck. Completely forgot to read the commented out scriptservice. Solved it instantly!

squashtomato
Автор

Thanks. Great tutorial as always. Love your style of tutoring. Waiting for the next tutorial

IlhamIsrafilov
Автор

Very good tutorial for beginners. Thanks. It helped me a lot. It explains very clearly and simply. Thanks for your effort.

dipankarghosh
Автор

Looking forward to your next tutorial. Great tutoring!!

somakroy
Автор

Thank you very much, helped me a lot this lesson.

developernader
Автор

You are great venkat!!! Waiting for more on this.

HistoryIR
Автор

Your tutorials are the best and easy to understand as well (Y)

saqlainraza
Автор

Thanks Sir You save me...God Bless you

fazalabbas
Автор

great tutorial, helped a lot, i have a question :
i have a webservice on a server what should i do to acces it online using jquery ?

mahethekiller
Автор

Hi, thank you for the informative tutorial,
what if you need to send more data to the webservice and the webservice is not int he same project. so employee would not be recongized

MrKary
Автор

Hello sir, thank you for this very clear and easy to follow explanation. I have one question about contentType/dataType parameter of ajax. My intellisense shows me contentType parameter, and when I set it to "xml", app doesn't work, error get caught and alert is shown. If I go to concole to see the error, this is shown: Request format is unrecognized for URL unexpectedly ending in '/GetEmployeeById'.
But if I put dataType:"xml" then everything work, but intellisense is not offering "dataType" parameter. Why is this?

braScene
Автор

thanks you... but this work excelent on localhost but didn't on the server... can you help me please?

ManeSalinasR
Автор

guys, shouldnt the "method" be GET here as its get request from the server? please someone explain it why it was POST instead of GET?

itspreethisworld
Автор

Hey, I tried doing the same but it throws exception while parsing the Json, The reason for that is the response that I get is a Mix of Json and some Junk, and when I try to destringify it throws an error. The response returned is as [{"category_name":"Motors", "cat_area_name":"Bizzarrini", "add_description":"Well maintained car"}, {"category_name":"Motors", "cat_area_name":"MakesAcuraAlfa", "add_description":"No Accidents"}]



<!DOCTYPE html>
<html>
<head>
<title>Server cannot clear headers after HTTP headers have been sent.</title>
<meta name="viewport" content="width=device-width" />
<style>
body .7em;color:black;}
p -5px}
b -5px}
H1 { }
H2 { }
pre {font-family:"Consolas", "Lucida Console", Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
@media screen and (max-width: 639px) {
pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
}
@media screen and (max-width: 479px) {
pre { width: 280px; }
}
</style>
</head>



Can you help provide me a resolution? Alternately how do we contact you?

johndsa
Автор

how to call a webservice method if there is two project in a solution, webservice is in different project and form is in different.

shibugope
Автор

Why don't we bind data to gridview?? Why this

jaswanthsingh
welcome to shbcf.ru