对Webapi的Ajax调用未达到Ajax响应且未返回结果

时间:2018-08-10 18:53:56

标签: ajax asp.net-web-api asp.net-web-api2 asp.net-ajax

我有一个webapi项目,我将在其中返回产品列表,并且在另一个项目中有一个ajax调用。

当我在webapi索引页面上使用ajax调用时,它会得到响应,而当我在引用webapi的项目中使用它时,它不会达到ajax的响应位。

在这两种情况下,均会调用api Get方法并返回正确的结果和响应,只是ajax不能在另一个项目上执行。

两个webapi索引页面上的ajax代码与另一个项目页面上的ajax代码完全相同:

Ajax:

$.getJSON("http://localhost:65331/dataapi/Product")
    .done(function (data) {
        // On the other project it never gets to this bit below:
        $.each(data, function (item) {
            var label = item.Name + ' (' + item.Code + ')';
        });
    });

WebAPI获取方法:

    public HttpResponseMessage Get()
    {
        ProductSearcher searcher = new ProductSearcher();
        List<ProductModel> products = searcher.GetResults();

        // Then I return the list
        return Request.CreateResponse(HttpStatusCode.OK, products);
    }

我也尝试使用$.ajax({type: "GET", ...}),但它没有任何改变。

有什么想法为什么能使ajax响应正常工作?

0 个答案:

没有答案