javascript – Http POST请求不止一次到达服务器

我开发了一个Web应用程序.
客户端 – Javascript / Typescript AngularJS
服务器端 – c# – asp.net web api 2
IIS Express作为主机.

我注意到一个奇怪的问题,即POST requets不止一次到达服务器.例如,一个请求被多次发送的场景,每个特定文件夹电子邮件的请求被发送3次.我还在“网络”选项卡中看到这些请求发送了三次.

public class mailsService extends ImailsService{

    static instance : ImailsService;

    // Dictionary of folder -> emails
    public Folders;


    public mailsService(){
        var scope = this;
        myInterval = setInterval(function(){
                    // Making this request until getting a successful response - waiting for server to go up
            scope.webService.get("url to get my own place").success(data => {
                scope.myLocation = data;
                scope.isLocationInit = true;
                getAllMails();
                window.cleanInterval(myInterval);
            })
        }, 3000);
    }

    // This function happen on it's own when running the client code.
    // The service is a singleton therefore this code runs only once.
    public getAllMails() {
    if (!this.isLocationInit)
        return;
    var scope = this;
    scope.webService.get("url to get all folders").then(reponse => {
        var folders = response.data;

        for (var i = 1; i <= folders.length; i ++){
                return scope.get("url to get the mails inside specific folder").then(initMails.bind(null, i));
        }});
    }

    public initMails (folderId : number, response){
        mailsService.instance.Folders[folderId].push(response.data);
    }
}

public class webService extends IwebService {
    public get(url : string){
        return this.$http.get(url);
    }

    public post(url : string, data : any){
        return this.$http.post(url, data);
    }
}


// Server side:

// Cross-Origin requests
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class MailsController : ApiController 
{
    private IMailsService _mailsService;

    public MailsController(IMailsService service)
    {
        this._mailsService = service;
    }

    public IHttpActionResult GetAllFolders()
    {
        var result = _mailsService.GetAllFolders();
        if (result == null)
            return BadRequest("...");

        return Ok(result);
    }

    public IHttpActionResult GetFolderEmails(int folderId)
    {
        var result = _mailsService.GetFolderEmails(folderId);
        if (result == null)
            return BadRequest("...");

        return Ok(result);
    }
}

控制器拟合方法命中两次甚至更多,尽管它仅从cliend侧代码调用一次.

这是可以配置的吗? IIS奇怪的事情?我真的不知道从哪里开始寻找.

最佳答案
在mailsS​​ervice中,您设置间隔计时器以每3秒调用Web服务.是否有可能在响应回来并完成整个处理时,计时器已经勾选了一次或两次并再次触发了程序?您可以通过将间隔更改为更短和更长的值来测试,并查看它是否会影响对服务器的调用次数.

降低风险的一种方法是在调用getAllMails之前,在成功回调函数的最开始时调用window.clearInterval:

scope.webService.get("url to get my own place").success(data => {
    window.clearInterval(myInterval);
    scope.myLocation = data;
    scope.isLocationInit = true;
    getAllMails();
}

另一种解决方案是避免使用setInterval.您可以执行scope.webService.get并等待响应.如果出现错误,您可以使用setTimeout在延迟后再次调用该服务:

public mailsService() {
    var scope = this;
    (function callWebService () {
        scope.webService.get("url to get my own place")
            .success(data => {
                scope.myLocation = data;
                scope.isLocationInit = true;
                getAllMails();
            })
            .error(data => { 
                setTimeout(callWebService, 3000); 
            });
    })();
}

注意:我不熟悉Typescript,因此可能有更好的方法来实现它.

转载注明原文:javascript – Http POST请求不止一次到达服务器 - 代码日志