我可以创建一个$http请求而不提交它吗?

时间:2021-10-10 23:00:31

I have a generic component in my system that deals with submitting HTTP requests in chunks. It takes care of some application-common headers that I need to attach and updating all the necessary places in the GUI.

我的系统中有一个通用组件,它处理以块形式提交HTTP请求。它处理一些应用程序通用的头文件,我需要附加和更新GUI中所有必要的位置。

This component is shared between couple other components for various use-cases. I'm trying to delegate the task of creating HTTP request from a given input to the callers but found out that when the caller does:

这个组件在不同用例的其他组件之间共享。我试图将创建HTTP请求的任务从给定的输入委托给调用者,但发现当调用者创建HTTP请求时:

var req = $http.get("url", {}) 

It's already submitting the request.

它已经提交了请求。

I'd like the callers to provide a method to generate a list of request objects from the input, and my component will deal with that later (add some headers for example, add success() and error() methods, or submit the requests in batches).

我希望调用者提供一个方法来从输入中生成请求对象列表,我的组件稍后将处理这个问题(添加一些header,例如添加success()和error()方法,或者批量提交请求)。

How can I only create the HTTP Request object, without sending it?

如何只创建HTTP请求对象而不发送它?

2 个解决方案

#1


2  

You can create wrapper function for "delayed" http request, that would return you preconfigured ready to call function, and use it instead of $http.

您可以为“延迟的”http请求创建包装器函数,该函数将返回您预先配置好的用于调用函数的函数,并使用它而不是$http。

Maybe like this:

也许是这样的:

function dHttp(config) {
    return function(options) {
       angular.merge(config, options);
       return $http(config);
    }
}

var req = dHttp({
    url: '/some', 
    data: {test: 21}
});

// later send it
req().then(function(data) { 
    console.log(data);
});

#2


0  

A plain AJAX can be useful here

简单的AJAX在这里很有用

xmlhttp.open("GET","a-url",true);
xmlhttp.send();

$http.get() is a declarative form of an ajax open and send.

get()是ajax打开和发送的声明形式。

var reqArr = [];
reqArr.push(xmlhttp.open("GET","a-url",true));
reqArr.push(xmlhttp.open("GET","b-url",true));
reqArr.push(xmlhttp.open("GET","c-url",true));

so you can change the objects as needed and send the ajax later on.

因此,您可以根据需要更改对象并在稍后发送ajax。

#1


2  

You can create wrapper function for "delayed" http request, that would return you preconfigured ready to call function, and use it instead of $http.

您可以为“延迟的”http请求创建包装器函数,该函数将返回您预先配置好的用于调用函数的函数,并使用它而不是$http。

Maybe like this:

也许是这样的:

function dHttp(config) {
    return function(options) {
       angular.merge(config, options);
       return $http(config);
    }
}

var req = dHttp({
    url: '/some', 
    data: {test: 21}
});

// later send it
req().then(function(data) { 
    console.log(data);
});

#2


0  

A plain AJAX can be useful here

简单的AJAX在这里很有用

xmlhttp.open("GET","a-url",true);
xmlhttp.send();

$http.get() is a declarative form of an ajax open and send.

get()是ajax打开和发送的声明形式。

var reqArr = [];
reqArr.push(xmlhttp.open("GET","a-url",true));
reqArr.push(xmlhttp.open("GET","b-url",true));
reqArr.push(xmlhttp.open("GET","c-url",true));

so you can change the objects as needed and send the ajax later on.

因此,您可以根据需要更改对象并在稍后发送ajax。