Reputation: 179
I want to retrieve a string from server using ajax jquery. I have done some code. But I don't know what is the mistake. I am not getting any value when i run. Here is my code
ReportForm.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="js\jquery-1.11.3.js"></script>
<script src="js\jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function ()
{
$('#btnEmployeeName').click(function () {
var empName = $('#txtID').val();
$.ajax({
url: 'EmployeeService.asmx/GetStringById',
data: { name: empName },
method: 'post',
dataType: 'xml',
success:function(data)
{
var jqueryXml = $(data);
$('showName').val(jqueryXml.find('name').text());
},
error:function(err)
{
alert(err);
}
})
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<center><h1>Report Form</h1>
<table class="auto-style1">
<tr>
<td><input type="button" value="Click me" id="btnEmployeeName" /></td>
<td><input type="text" id="txtID" /></td>
</tr>
<tr>
<td><input type="text" id="showName" /> </td>
<td> </td>
</tr>
</table>
</center>
</div>
</form>
EmployeeService.asmx.cs
namespace NibrassSample
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class EmployeeService : System.Web.Services.WebService
{
[WebMethod]
public Employee GetStringById(string name)
{
Employee emp = new Employee();
emp.name = name;
return emp;
}
}
}
Employee.cs
[assembly: OwinStartup(typeof(NibrassSample.Employee))]
namespace NibrassSample
{
public class Employee
{
public string name { get; set; }
}
}
I am new to jquery. please help me, thanks.
Upvotes: 1
Views: 107
Reputation: 17049
Your AJAX call and the C# [WebMethod]
look fine but I see in your success function you are trying to populate a text box with the returned value but you're not selecting it correctly in jQuery, you're doing this:
$('showName').val(jqueryXml.find('name').text());
You should put the # character before showName
.In jQuery this means you're selecting the element by id:
$('#showName').val(jqueryXml.find('name').text());
Upvotes: 1