【发布时间】:2018-12-14 21:58:00
【问题描述】:
我创建了 WebApi 项目并拥有 Get 方法。
public HttpResponseMessage Get()
{
List<Student> students;
List<StudentVM> studentsvm = new List<StudentVM>();
using (SchoolEntities sc = new SchoolEntities())
{
students= sc.Students.ToList<Student>();
}
foreach (Student s in students)
{
StudentVM svm = new StudentVM { Sid = s.Id, Sname = s.Name };
studentsvm.Add(svm);
}
return Request.CreateResponse(HttpStatusCode.OK, studentsvm);
}
通过 URL 请求 http://localhost:3735/api/values 我得到数据。我什至在这里设置了一个断点。因此,当刷新上述 URL 时,断点命中。
在同一个项目中,我创建了一个 html 页面并具有以下代码。
<script type="text/javascript">
$(document).ready(function (e)
{
var ulemp = $('#ulemp');
$('#showemp').click(function () {
$.ajax({
type: 'GET',
Url: 'http://localhost:3735/api/values',
datatype: 'json',
success: function (data, jqXHR) {
console.log(jqXHR);
ulemp.empty();
ulemp.append(data.value);
},
error: function () {
console.log("There is an Error");
}
})
});
});
</script>
<body>
<button id="showemp" name="showbtn"> Show Employees</button>
<button id="clear" name="clearbtn">Clear</button>
<p id="ulemp"></p>
</body>
但我没有收到任何错误或任何响应。我经历了许多与 WebApi 和 Ajax 调用相关的堆栈溢出问题。大部分问题都没有解决,部分解决的有小错误,我已经验证过了。
目前我正在使用 VS2012 和 jquery 1.7.1
如果有人能指出我所缺少的东西,我真的很感激。
【问题讨论】:
-
这个问题你解决了吗?
-
@PrashantPimpale 当我在另一台计算机上执行相同的项目时,它运行良好。我不确定 Visual Studio 的特定实例中存在什么问题。
-
因此,我的意思是顺便解决了。
-
好.. 因为 我正在关注这个 kudvenkat 教程 行,只是想确保得到解决方案!对我来说,他是 .net 世界上最好的人!
标签: ajax asp.net-web-api