Clientside: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ApiTabletClientWeb.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="Scripts/jquery-1.8.2.js"></script>
<script type="text/javascript" src="Scripts/jquery-1.8.2.min.js"></script>
<title></title>
</head>
<body>
<script>
jQuery.support.cors = true;
$(document).ready(function () {
$.ajax({
url: "localhost:9320/api/Login",
type: "Get",
dataType: 'json',
async: "true",
traditional:true,
success: function (data) {
$('#usersection').empty();
for (var i = 0; i < data.length; i++) {
$("<tr><td>" + data[i].CenterCode + "</td><td>" + data[i].LoanCode + "</td><td>" + data[i].ClientCode + "</td><td>" + data[i].ClientName + "</td><td>" + data[i].DateApproved + "</td><td>" + data[i].LoanStatus + "</td></tr>").appendTo("#usersection");
}
},
error: function (msg) { alert(msg); }
});
});
</script>
<form id="form1" runat="server">
<div>
<h2>My Address Book</h2>
<br />
<table>
<thead>
<tr>
<td>CenterCode</td>
<td>LoanCode</td>
<td>ClientCode</td>
<td>ClientName</td>
<td>DateApproved</td>
<td>LoanStatus</td>
</tr>
</thead>
<tbody id="usersection">
</tbody>
</table>
<br />
<br />
<h2> Add A Contact</h2>
<table>
<tr>
<td>First Name</td>
<td>
<asp:TextBox runat="server" ID="txtFirstName" /></td>
</tr>
<tr>
<td>Last Name</td>
<td>
<asp:TextBox runat="server" ID="txtLastName" /></td>
</tr>
<tr>
<td>Company</td>
<td>
<asp:TextBox runat="server" ID="txtCompany" /></td>
</tr>
<tr>
<td>Phone</td>
<td>
<asp:TextBox runat="server" ID="txtPhone" /></td>
</tr>
<tr>
<td>Email</td>
<td>
<asp:TextBox runat="server" ID="txtEmail" /></td>
</tr>
<tr>
<td>
<asp:Button Text="Save" runat="server" ID="btnSave" />
</td>
</tr>
</table>
<br />
<br />
<asp:TextBox ID="txtSearch" runat="server" />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClientClick="search();return false;" />
<br />
<br />
<p id="searchResult" >
</p>
<br />
<br />
<asp:TextBox ID="txtDelete" runat="server" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClientClick="deleteuser();return false;" />
</div>
</form>
</body>
</html>
Server side code API
public class LoginController : ApiController
{
#region Post Method Write Data to Server and return response to client
string BranchCode =null;
List<clientliststructure> ObjclientList = new List<clientliststructure>();
// GetDetail([FromUri] string info)
public List<clientliststructure> Get()
{
// LoginSructure objLoginStructure=new LoginSructure();
//objLoginStructure.StaffCode = "11383";
//objLoginStructure.Password = "1234";
try
{
string info = "11014 123";
string[] words = info.Split(' ');
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionStrig"].ConnectionString;
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("select BranchCode from Staff where StaffCode='" + words[0] + "' and Password='" + words[1] + "'", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
BranchCode = reader.GetString(0);
}
reader.Close();
cmd.Dispose();
con.Close();
ObjclientList = new MapClientDatatoModel().Mapclientlist(BranchCode);
return ObjclientList;
}
catch (Exception ex)
{
return ObjclientList;
}
}
#endregion
}
error
XMLHttpRequest cannot load http://localhost:9320/api/Login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:6515' is therefore not allowed access.