XMLHttpRequest无法在Chrome浏览器中加载 [英] XMLHttpRequest cannot load in chrome Browser

查看:84
本文介绍了XMLHttpRequest无法在Chrome浏览器中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Clientside:Default.aspx



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>





服务器端代码API



公共类LoginController: ApiController

{

#region Post方法将数据写入服务器并将响应返回给客户端

string BranchCode = null;

List< clientliststructure> ObjclientList = new List< clientliststructure>();

// GetDetail([FromUri] string info)

public List< clientliststructure>获取()

{

// LoginSructure objLoginStructure = new LoginSructure();

//objLoginStructure.StaffCode =11383;

//objLoginStructure.Password =1234;

尝试

{

string info =11014 123;

string [] words = info.Split('');

string connectionstring = ConfigurationManager.ConnectionStrings [ConnectionStrig]。ConnectionString;

SqlConnection con = new SqlConnection(connectionstring);

SqlCommand cmd = new SqlCommand(从Staff中选择BranchCode,其中StaffCode ='+ words [0] +'和Password ='+ words [ 1] +',con);

con.Open();

SqlDataReader reader = cmd.ExecuteReader();

while (读者阅读())

{

BranchCode = reader.GetString(0);

}

reader.Close();

cmd.Dispose();

con.Close();

ObjclientList = new MapClientDatatoModel()。Mapclientlist(BranchCode);

return ObjclientList;

}

catch(例外情况)

{

返回ObjclientList;

}



}



#endregion

}





错误

XMLHttpRequest无法加载http:// localhost:9320 / api / Login。请求的资源上不存在Access-Control-Allow-Origin标头。因此不允许原点'http:// localhost:6515'访问。



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.

推荐答案

(document).ready(function(){
(document).ready(function () {


.ajax({
url: localhost:9320 / api / Login
类型: 获取
dataType: json'
async true
traditional: true
成功:函数(数据){
.ajax({ url: "localhost:9320/api/Login", type: "Get", dataType: 'json', async: "true", traditional:true, success: function (data) {


' #usersection')。empty();
for var i = <温泉n class =code-digit> 0 ; i < data.length; i ++){
('#usersection').empty(); for (var i = 0; i < data.length; i++) {


这篇关于XMLHttpRequest无法在Chrome浏览器中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆