Click here to Skip to main content
15,898,987 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Clientside: Default.aspx

XML
<%@ 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.
Posted
Comments
xszaboj 5-Jun-15 7:39am    
I think this is because you are asking different server(different Origin), you have to allow this ;) just google cross origin requests
Richard Deeming 5-Jun-15 9:10am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
amt kumar rai 6-Jun-15 17:13pm    
I ACCEPTED BUT MY QUESTION IS THAT WHEN SAME CODE RUN IN IE ITS WORK FINE BUT IN GOOGLE CHROME ERROR OCCUR "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."

1 solution

Hi,

Just for quick reference see the following urls

http://enable-cors.org/server_iis7.html

and

http://enable-cors.org/server_aspnet.html


also look for JSONP
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900