如果我刷新页面,POST不会完成(POST doesn't complete if I refresh page)
我有一个表单,在提交时使用$ .ajax来调用将数据写入文本文件的bash脚本。
在Firefox中,如果我立即刷新页面或导航到其他页面,POST将中止,文本文件不会更新:
[15:07:12.781] POST http://server/writegit.cgi [undefined 891ms]
如果我在刷新之前等待一两秒钟,POST就完成了,一切都很好:
[15:03:04.995] POST http://server/writegit.cgi [HTTP / 1.1 200 OK 2022ms]
但是,Chrome中不会出现此问题 - 一旦表单提交,即使我立即刷新页面,POST也会完成。
有什么方法可以让Firefox像Chrome一样?
I have a form which uses $.ajax on submit to call a bash script that writes data to a text file.
In Firefox, if i immediately refresh the page or navigate to a different page, the POST aborts, and the text file does not update:
[15:07:12.781] POST http://server/writegit.cgi [undefined 891ms]
If I wait a second or two before refreshing, the POST completes and everything is fine:
[15:03:04.995] POST http://server/writegit.cgi [HTTP/1.1 200 OK 2022ms]
However, this problem does not happen in Chrome-- once the form submits, the POST will complete even if I refresh the page immediately.
Is there any way I can make Firefox behave like Chrome?
原文:https://stackoverflow.com/questions/9608398
最满意答案
所以对于这个用例,你应该使用工厂而不是服务。
在大多数使用
factory
方法的情况下,它实际上应该使用service
方法,并成为ES6中的一个class
。 这种情况是例外。 您需要注册一个调用$resouce factory
的工厂方法。在ES6中(如果用require替换import,大多数是ES5),它应该看起来像这样:
import {app} from '../app'; feedLoadFactory.$inject = ['$resource']; feedLoadFactory($resource) { return $resource('http://ajax.googleapis.com/ajax/services/feed/load', {}, { fetch: { method: 'JSONP', params: {v: '1.0', callback: 'JSON_CALLBACK'} } }); } app.factory('feedLoad', feelLoadFactory);
然后你可以注入它作为
feedLoad
并使用它作为feedLoad.fetch...
So for this use case you should be using a factory rather than a service.
In most cases where the
factory
method is used, it actually should use theservice
method and be aclass
in ES6. This case is the exception. You need to register a factory method that calls the$resouce factory
.In ES6 and (and mostly ES5 if you replace import with require) it should look something like this:
import {app} from '../app'; feedLoadFactory.$inject = ['$resource']; feedLoadFactory($resource) { return $resource('http://ajax.googleapis.com/ajax/services/feed/load', {}, { fetch: { method: 'JSONP', params: {v: '1.0', callback: 'JSON_CALLBACK'} } }); } app.factory('feedLoad', feelLoadFactory);
Then you can inject it as
feedLoad
and use it asfeedLoad.fetch...
相关问答
更多-
所以对于这个用例,你应该使用工厂而不是服务。 在大多数使用factory方法的情况下,它实际上应该使用service方法,并成为ES6中的一个class 。 这种情况是例外。 您需要注册一个调用$resouce factory的工厂方法。 在ES6中(如果用require替换import,大多数是ES5),它应该看起来像这样: import {app} from '../app'; feedLoadFactory.$inject = ['$resource']; feedLoadFactory($ ...
-
在tsconfig.json将module值从当前的任何值更改为es6 。 当TypeScript转换TS文件时,它将在ES6中完成。 如果您需要删除TypeScript支持并依赖纯JavaScript,则可以使用已转换的代码进行进一步的工作。 更新 如果要生成ES6代码,则需要设置target ,而不是module 。 module处理加载器。 您可以重用和更新的已转换代码的示例如下所示: var __decorate = (this && this.__decorate) || function (de ...
-
本教程似乎使用相同的API。 这是一个似乎有用的参考实现 。 看看这个文件 。 注意作者在第1行中如何将库作为jtt_openweathermap导入到他的角度模块中? 特定的代码行看起来像这样: angular.module('myApp', ['ui.router', 'ngGeolocation', 'ngProgress', 'ui.router.title', 'jtt_openweathermap', 'ngMap']) 你也需要这样做。 我想你可能不是那样做的。 我还会在这里看一下这个控制器 ...
-
角度constant服务只是一种在配置阶段可用的服务,是存储模块常量的建议方式(因此名称)。 它不是一个真正的常量,可以随时用$provide.constant覆盖。 链式angular.module(...)导出Module对象 ,在非Angular上下文中没有意义。 为Angular模块和ES6模块保留单独的导出。 导出Angular模块name属性是一种常规方法,该属性可以在语义上导入并在其他模块中使用。 export const foo = ...; export default angular. ...
-
看起来你试图在保存范围之前调用使用“$ scope”的函数。 尝试移动this.scope = $ scope; 高于this.getEnterpriseData():在你的代码中: class EnterpriseController { /*@ngInject*/ constructor(enterpriseService, $scope) { this.name = 'enterprise'; this.systemId = 20003 this.pageLink = ...
-
在ES6类属性上设置Angular服务会影响其功能(Setting an Angular service on an ES6 class property affects its functionality)[2022-02-11]
你以错误的方式调用put函数。 尝试使用此参数顺序: this.$cookieStore.put('authenticatedAccount', JSON.stringify(account)); You call the put function the wrong way. Try with this parameter order: this.$cookieStore.put('authenticatedAccount', JSON.stringify(account)); -
您的代码中存在语法错误... - 不允许在类体中使用const声明(在当前语法中,您只能在其中定义方法)。 相反,您需要将此const声明为对象属性,或将其从类声明中移出。 You have syntax error in your code… - it is not allowed to have const declaration in class body(in current syntax you can define only methods there). Instead, you need t ...
-
对于不需要使用DI service类,不应使用。 相反,服务可以定义为value并包含构造函数: class ApartmentsGridPaginator { constructor(pageSize, navRange, data) {} } app.value('ApartmentsGridPaginator', ApartmentsGridPaginator); ... class ApartmentsController { constructor(ApartmentsGridPag ...
-
eventSrv服务应该是service : angular.module(moduleName, []) .service('eventSrv', EventService); factory服务应该与工厂函数一起使用,工厂函数由注入器直接调用,ES6类应该是new编辑的。 eventSrv service should be service: angular.module(moduleName, []) .service('eventSrv', EventService); fact ...
-
我解决了自己的问题 将PageController更改为: class PageController { constructor($scope, $state, GlobalLoading){ this.$scope = $scope; this.$state = $state; //GlobalLoading is the service I injected this.globalLoading = Glob ...