Javascript 浏览器可以在处理post请求之前处理脚本
浏览器可以在处理post请求之前处理脚本。考虑到我有以下样品Javascript 浏览器可以在处理post请求之前处理脚本,javascript,angularjs,post,restangular,Javascript,Angularjs,Post,Restangular,浏览器可以在处理post请求之前处理脚本。考虑到我有以下样品 if (some true condition) { console.log("ready to post") restangular.all.post(RequestData).then(function(response){ //some post methods console.log("response") }); } //then containing scripts
if (some true condition) {
console.log("ready to post")
restangular.all.post(RequestData).then(function(response){
//some post methods
console.log("response")
});
}
//then containing scripts
cosole.log('Hello')
....
输出
准备发布
你好
回应
我希望在打印“你好”之前完成POST请求。如何克服这个问题?为了实现您想要的,您应该研究angularJS承诺,因为POST请求是异步的。例如,请检查此链接: 主要思想是首先创建一个返回延迟对象的调用,比如
this.myFunction = function (myForm) {
var deferred = $q.defer();
$http.post(myURL, myForm)
.success(function (data) {
//resolve the promise
deferred.resolve('SUCCESS');
})
.error(function (data) {
//reject the promise
deferred.reject('ERROR');
});
//return the promise
return deferred.promise;
}
然后像这样称呼它
var myPromise = this.myFunction ($scope.modalForm);
// wait until the promise return resolve or eject
//"then" has 2 functions (resolveFunction, rejectFunction)
myPromise.then(function(resolve){
// do stuff here, the post request is successfully finished
}, function(reject){
return;
});
或者,在POST请求之后要执行的任何代码,都可以将其放入请求的成功函数中。将“Hello”放在“response”旁边?!异步执行是按原样进行的,这就是它的工作方式,这就是为什么会出现类似
。那么…请重新表述这个问题好吗?我不知道你在问什么。