使用javascript/jQuery使用Web API
我正在尝试使用一个在这里找到的实时web api使用javascript/jQuery使用Web API,javascript,c#,jquery,asp.net,model-view-controller,Javascript,C#,Jquery,Asp.net,Model View Controller,我正在尝试使用一个在这里找到的实时web api http://dummy.restapiexample.com/api/v1/employee/1 但我的代码从未成功,总是会出错 请问我错过了什么 <title>ASP.NET Web API</title> <script src="~/Scripts/jquery-3.3.1.js"></script> <script type="text/javascript">
http://dummy.restapiexample.com/api/v1/employee/1
但我的代码从未成功,总是会出错
请问我错过了什么
<title>ASP.NET Web API</title>
<script src="~/Scripts/jquery-3.3.1.js"></script>
<script type="text/javascript">
function GetAllEmployees()
{
jQuery.support.cors = true;
$.ajax({
url: 'http://dummy.restapiexample.com/api/v1/employee/1',
type: 'GET',
dataType: 'json',
success: function (data) {
WriteResponse(data);
},
error: function (data) {
alert("Error");
}
});
}
</script>
</head>
<body id="body">
<div>
<label for="itId">ID:</label>
<input type="text" id="custId" size="5" />
<input type="text" id="locId" size="5" />
<input id="getPerson" type="button" value="Search" onclick="GetAllEmployees();return false;" />
<input type="text" class="form-control" id="getPersonResult" />
<p id="item" />
</div>
</body>
</html>
ASP.NET Web API
函数GetAllEmployees()
{
jQuery.support.cors=true;
$.ajax({
网址:'http://dummy.restapiexample.com/api/v1/employee/1',
键入:“GET”,
数据类型:“json”,
成功:功能(数据){
写响应(数据);
},
错误:函数(数据){
警报(“错误”);
}
});
}
身份证件:
对于js和jquery,我需要包括哪些参考库
或者我遗漏了什么语法 什么是书面回应?我相信这是一个asp.net的东西。将其替换为警报(数据),然后查看您是否收到任何信息。如果请求失败,您需要调查原因。您可以在(Chrome)中看到这一点开发者控制台和网络面板中的url请求。@Caharpuka从源站“”访问“employee/1”处的XMLHttpRequest已被CORS策略阻止:请求的资源上不存在“Access Control Allow origin”标头。这很奇怪,因为我可以用XMLHttpRequest检索此url,并且它有CORS标头使用Access Control Allow Origin:*我尝试使用自己的不同web api,我安装了Cors并添加了一个标题,但它不起作用