Javascript 当调用Web Api Get方法时,Ajax调用不起作用。你知道吗,我错过了什么?
我在服务器端的Javascript 当调用Web Api Get方法时,Ajax调用不起作用。你知道吗,我错过了什么?,javascript,jquery,ajax,web-services,asp.net-web-api,Javascript,Jquery,Ajax,Web Services,Asp.net Web Api,我在服务器端的valuescoontroller中有一个get方法,这里使用的是mvc4webapi 方法 [HttpGet] public List<Product> Get() { return productRepository.ListOfProducts().ToList(); } [HttpGet] 公共列表Get() { 返回productRepository.ListOfProducts().T
valuescoontroller
中有一个get方法,这里使用的是mvc4webapi
方法
[HttpGet]
public List<Product> Get()
{
return productRepository.ListOfProducts().ToList();
}
[HttpGet]
公共列表Get()
{
返回productRepository.ListOfProducts().ToList();
}
我已经通过ajax调用了这个方法
$.ajax({
url: "http://localhost:57260/api/values",
type: "GET",
dataType: 'json',
success: function (data) {
alert("Ramesh is a yummy ;)");
for (var i = 0; i < data.length; i++) {
$("<tr></tr><td></td>" + data[i].Name + "</td><td></td>" + data[i].Address + "</td><td></td>" + data[i].DOB + "</td></tr>").appendTo("#tbPerson");
}
},
error: function (msg) {
alert("Ramesh is a fool");
}
});
$.ajax({
url:“http://localhost:57260/api/values",
键入:“获取”,
数据类型:“json”,
成功:功能(数据){
警惕(“Ramesh是一个美味的;”);
对于(变量i=0;i
这种ajax方法总是去失败函数,而不是去成功方法
但当我在浏览器中键入url时,它将返回json数据。请参见此屏幕
但是为什么ajax不起作用呢?这很奇怪
是的,拉梅什
更新:
我看不到控制台错误详细信息,因为我使用的是Windows mobile app,所以它在移动仿真器上运行根据您提供的详细信息,这很可能是由于同源策略限制。谷歌搜索“ASP.NET Web API CORS”。这将起作用
$.ajax({
url: "http://localhost:57260/api/values",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
function (data) {
alert("Ramesh is a yummy ;)");
for (var i = 0; i < data.length; i++) {
$("<tr></tr><td></td>" + data[i].Name + "</td><td></td>" + data[i].Address + "</td><td></td>" + data[i].DOB + "</td></tr>").appendTo("#tbPerson");
}
},
error: function (msg) {
alert("Ramesh is a fool");
}
});
$.ajax({
url:“http://localhost:57260/api/values",
方法:“获取”,
标题:{“Accept”:“application/json;odata=verbose”},
功能(数据){
警惕(“Ramesh是一个美味的;”);
对于(变量i=0;i
检查firebug中的任何错误web浏览器控制台中的任何错误(检查firebug)@Rex我看不到控制台,因为我使用的是Windows mobile app,Sop正在移动模拟器上运行。Ori将使用fiddler进行检查,请稍等几分钟,Guy's我不能,因为fiddler无法获取模拟器进程:(