在 Angular 中将 JavaScript 函数转换为$http提供程序
Converting javascript function into $http provider in Angular
我有以下函数,我正在尝试使用$http提供程序重写该函数。文档显示了许多不同的方法,我无法正确处理。这是函数:
function Ingest(Filename, ID, baseUrl, logger){
var url = baseUrl + '/php/' + 'Ingest.php';
var dataString = 'Filename=' + encodeURIComponent(Filename) + '&ID=' + encodeURIComponent(ID);
$.ajax({
type: "POST",
url: url,
async: true,
cache: false,
data: dataString,
success: function(results){
logger.success('Ingestion process has been finished.', '', 'Success');
}
//fail
, error: function (jqXHR, textStatus, errorThrown){
alert("error:'r'n" + errorThrown);
}
});
}
下面是$http代码的示例:
$http({
method: 'POST',
url: config.appBaseUrl + '/php/' + 'Ingest.php',
data: { ID: encodeURIComponent(ID), Filename: encodeURIComponent(Filename) }
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
谢谢
在第一个样本中,你做一个帖子,在第二个样本中做一个get。
您可以使用$http提供的快捷方式方法。
$http.post( config.appBaseUrl + '/php/' + 'Ingest.php', Filename: encodeURIComponent(Filename), ID: encodeURIComponent(ID)).then(function(response){
}, function(rejection){
});
如果要为$http(标头,...)设置一些特定的配置,请使用函数的第三个参数。
请注意,快捷方式 post/put 有第二个参数用于请求正文,第三个参数用于配置。删除和获取没有请求正文参数,因此配置是函数的第二个参数。
相关文章:
- $http.delete回调未在基本CRUD应用程序中启动
- Pinterest API应用程序处于开发模式(因此我可以使用http重定向)
- 我可以在React应用程序中使用什么作为Angular的等价物;s$http.get
- Angular提供程序和$http
- GWT应用程序中出现错误HTTP 404(“Script Tag Failure-no status available
- node.js-express.js API删除http后处理程序
- Angularjs提供程序:在返回之前解决$http请求
- 从Windows8JS应用程序获取仅HTTP cookie
- ES5上的Angular2 Http提供程序(不是TypeScript)
- 如何进行服务器端HTTP重定向更新引用程序(更改原始引用程序)
- 如何记录Apache Cordova应用程序中加载的iframe的http响应
- HTTP 请求后,数据未显示在 Ember 应用程序中
- 如何检查提供程序以查看 HTTP 数据是否已准备就绪
- 对于Chrome扩展程序,从https站点调用http站点时出现错误
- 如何检查“http://”是否包含在 Angular .js 应用程序的 url 输入中
- 区分对$http的应用程序调用和 Angular 对拦截器中静态资源的请求
- 为什么 HTTP 图像没有出现在 HTTPS 应用程序中
- $http AngularJS 应用程序初始化之前的请求
- 从 http 处理程序返回的 JSON 在 IE8 中为空,但在 IE10 或 Chrome 中不是
- 使用 ajaxError 全局处理程序显示 HTTP 状态代码 401 详细信息