C# $http.get未反映数据库更改

C# $http.get未反映数据库更改,c#,asp.net,angularjs,wcf,C#,Asp.net,Angularjs,Wcf,我有一个运行AngularJS的ASP.NET项目,该项目正在访问使用数据库的WCF服务。这是一个简单的CRUD界面,其中有一个表,显示数据库数据以及用于添加、编辑和删除数据的选项。在用户添加(即:$http.post)、编辑(即:$http.put)或删除条目(即:$http.delete)后,我的控制器调用$http.get,并使用它更新表。所有请求都使用“promise”结构,并将捕获错误 有时在通过请求后,表在更改后不会更新。我已经检查过了,从get请求检索到的数据不包括更改。但是,如果

我有一个运行AngularJS的ASP.NET项目,该项目正在访问使用数据库的WCF服务。这是一个简单的CRUD界面,其中有一个表,显示数据库数据以及用于添加、编辑和删除数据的选项。在用户添加(即:
$http.post
)、编辑(即:
$http.put
)或删除条目(即:
$http.delete
)后,我的控制器调用
$http.get
,并使用它更新表。所有请求都使用“promise”结构,并将捕获错误

有时在通过请求后,表在更改后不会更新。我已经检查过了,从get请求检索到的数据不包括更改。但是,如果我重新加载页面,更改将出现。为什么会发生这种情况,为什么只是偶尔发生?
post
/
put
/
delete
请求和get请求之间是否需要延迟或什么?这是服务器问题还是客户端问题?提前谢谢

编辑1

在更新失败期间检查chrome dev tools网络中的实际请求时,我发现对create/delete/update服务的两个调用将在GET请求之后进行。第一个始终是选项请求,后面是实际的POST/DELETE/PUT请求。早些时候,为了让这些服务与我的界面一起工作,我必须使用以下内容修改services Global.asax.cs文件:

protected void Application_BeginRequest(object sender, EventArgs e)
{
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
}
问题可能就在这里。有什么想法吗

编辑2

“我的控制器”中的“保存”功能:

$scope.save = function () {
    // Store user inputs
    var Map = {
        Id: $scope.Id,
        Channel: $scope.Channel,
        SubChannel: $scope.SubChannel,
        ChannelStatus: $scope.ChannelStatus,
        MappedStatus: $scope.MappedStatus
    };
    var post = CRUD_AngularJS_RESTService.post(Map);
    post.then(function (pl) {
        // Clear input forms
        ClearModels();
        $scope.ResultMessage = "New entry " + pl.data.Id + " added";
        $scope.AddEntry = false;
        // Re-populate table with new data
        GetFullMap();
    },
        function (err) {
            console.log("Error: ", err);
        });
};
function GetFullMap() {
    var get = CRUD_AngularJS_RESTService.getFullMap();
    get.then(function (pl) {
        console.log("here ", pl.data);
        // Display map data in table
        $scope.Maps = pl.data;
        console.log($scope.Maps);
    },
        function (errorPl) {
            $scope.ResultMessage = "Error loading map";
            console.log("Error occurred while retrieving map ", errorPl);
        });
}
GetFullMap()是控制器中的另一个函数:

$scope.save = function () {
    // Store user inputs
    var Map = {
        Id: $scope.Id,
        Channel: $scope.Channel,
        SubChannel: $scope.SubChannel,
        ChannelStatus: $scope.ChannelStatus,
        MappedStatus: $scope.MappedStatus
    };
    var post = CRUD_AngularJS_RESTService.post(Map);
    post.then(function (pl) {
        // Clear input forms
        ClearModels();
        $scope.ResultMessage = "New entry " + pl.data.Id + " added";
        $scope.AddEntry = false;
        // Re-populate table with new data
        GetFullMap();
    },
        function (err) {
            console.log("Error: ", err);
        });
};
function GetFullMap() {
    var get = CRUD_AngularJS_RESTService.getFullMap();
    get.then(function (pl) {
        console.log("here ", pl.data);
        // Display map data in table
        $scope.Maps = pl.data;
        console.log($scope.Maps);
    },
        function (errorPl) {
            $scope.ResultMessage = "Error loading map";
            console.log("Error occurred while retrieving map ", errorPl);
        });
}
在Services.js中,post功能:

// Create new record
this.post = function (Map) {
    var request = $http({
        method: "post",
        url: "http://localhost:63647/Services.svc/rest/MapCreate",
        data: Map
    });
    return request;
}
this.getFullMap = function () {
    return $http.get("http://localhost:63647/MerchantServices.svc/rest/FullMapRead");
};
app.controller("CRUD_AngularJS_RESTController", function($scope, CRUD_AngularJS_RESTService, $timeout)
{
     ...
});
同样在服务中,get函数:

// Create new record
this.post = function (Map) {
    var request = $http({
        method: "post",
        url: "http://localhost:63647/Services.svc/rest/MapCreate",
        data: Map
    });
    return request;
}
this.getFullMap = function () {
    return $http.get("http://localhost:63647/MerchantServices.svc/rest/FullMapRead");
};
app.controller("CRUD_AngularJS_RESTController", function($scope, CRUD_AngularJS_RESTService, $timeout)
{
     ...
});

你使用哪种浏览器

旧ie版本(ie9)有一个积极的缓存策略。 有时它会缓存您的get http请求

您必须在服务器响应的头中设置一个no-cache参数,或者只向每个请求添加一个timestamp参数(以便每个请求都不同)


您使用哪种浏览器

旧ie版本(ie9)有一个积极的缓存策略。 有时它会缓存您的get http请求

您必须在服务器响应的头中设置一个no-cache参数,或者只向每个请求添加一个timestamp参数(以便每个请求都不同)


我不确定为什么会发生这种情况,但这种解决方法似乎可以解决问题,我只是在调用GetFullMap()之前增加了一点延迟:

还要确保在app.controller函数中提供$timeout:

// Create new record
this.post = function (Map) {
    var request = $http({
        method: "post",
        url: "http://localhost:63647/Services.svc/rest/MapCreate",
        data: Map
    });
    return request;
}
this.getFullMap = function () {
    return $http.get("http://localhost:63647/MerchantServices.svc/rest/FullMapRead");
};
app.controller("CRUD_AngularJS_RESTController", function($scope, CRUD_AngularJS_RESTService, $timeout)
{
     ...
});

不确定为什么会发生这种情况,但这种解决方法似乎可以解决问题,我只是在调用GetFullMap()之前增加了一点延迟:

还要确保在app.controller函数中提供$timeout:

// Create new record
this.post = function (Map) {
    var request = $http({
        method: "post",
        url: "http://localhost:63647/Services.svc/rest/MapCreate",
        data: Map
    });
    return request;
}
this.getFullMap = function () {
    return $http.get("http://localhost:63647/MerchantServices.svc/rest/FullMapRead");
};
app.controller("CRUD_AngularJS_RESTController", function($scope, CRUD_AngularJS_RESTService, $timeout)
{
     ...
});

检查dev tools网络中的实际请求,可能会被缓存,这将是一个304状态。检查你的标题also@charlietfl我刚查过这个。。。加载页面时,第一个GET请求得到响应200。添加新条目时,会有一个POST(带有响应200),后跟一个GET(响应200)。当页面没有正确更新时,实际上有两个对create服务的调用;第一个是选项请求,另一个是帖子。这可能是相关的,对吧?选项是跨域飞行前请求。为什么不在第一次请求中返回数据?还显示代码,在发出第二个请求之前是否正在等待第一个请求?@charlietfl谢谢,请参阅编辑2检查开发工具网络中的实际请求,可能会被缓存,这将是一个304状态。检查你的标题also@charlietfl我刚查过这个。。。加载页面时,第一个GET请求得到响应200。添加新条目时,会有一个POST(带有响应200),后跟一个GET(响应200)。当页面没有正确更新时,实际上有两个对create服务的调用;第一个是选项请求,另一个是帖子。这可能是相关的,对吧?选项是跨域飞行前请求。为什么不在第一次请求中返回数据?同时显示代码,在发出第二个请求之前是否正在等待第一个请求?@charlietfl谢谢,请参阅使用Chrome编辑2。。。服务器响应的标头中已经没有缓存参数。正在使用Chrome。。。服务器响应的标头中已经没有缓存参数。