向 WebApi 发出 ajax 请求时出现错误 0

Error 0 while making ajax request to WebApi

我正在向我的 WebApi 发出 ajax 请求 并且在页面加载后我在回调中收到错误 0 而我正在正确 json 时url 点击 WebApi。

当我检查 chrome's consol 选项卡中的页面时,显示 url 已更改。我在我的代码中请求 http://localhost:1960/api/Product/GetProducts,在 chrome 中它变为 http://localhost:1960/api/Product/GetProducts?_=1534696122451

请建议需要做什么。

代码

var dataTable = $('#productTable').DataTable({
        "ajax": {
            "url": 'http://localhost:1960/api/Product/GetProducts',
            "type": "GET",
            "datatype": "json",
            //"success": function (result, status, xhr) {
            //    alert("Success : " + result);
            //},
            "error": function (xhr, status, error) {
                alert("Failed : Result: " + status + " - " + error + " | " + xhr.status + " - " + xhr.statusText);
            }
        },

        "columns": [
            { "data": "productName", "autoWidth": true },
            { "data": "productCode", "autoWidth": true },
            { "data": "productDescription", "autoWidth": true },
        ],

        "language": {
            "emptyTable": "No Data found, Please Click on <b><i>Add New</i></b> Button to Add Product Record in System."
        }
    }); 

还有 This is state that my client mvc project is hosted in different port. 所以这是问题吗?

作为一项安全措施,浏览器会阻止任何非同一来源的非安全 (http) 请求。这里您的网站 api 托管在端口 1960 上,您的 Web 应用程序托管在 2640 上,这就是您收到此错误的原因。

您可以通过此 link 了解有关 CORS 的更多信息以及如何使您的 API 能够接收 CORS 请求:

Enabling Cross-Origin Requests in ASP.NET Web API 2