Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当调用Web Api Get方法时,Ajax调用不起作用。你知道吗,我错过了什么?_Javascript_Jquery_Ajax_Web Services_Asp.net Web Api - Fatal编程技术网

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无法获取模拟器进程:(