设置:我希望有一个服务,多个控制器可以查询使用$ http提取的数据.最初的解决方案是使用此处建议的promises .
问题:每次控制器查询服务时,服务都会返回$ http保证,从而导致多次查询只是从远程服务器中反复提取相同的数据.
解决方案:服务函数返回数据或下面的承诺.并由控制器进行相应的检查和操作.
app.factory('myService', function($http) {
var items = [];
var myService = {
getItems: function() {
// if items has content, return items; otherwise, return promise.
if (items.length > 0) {
return items;
} else {
var promise = $http.get('test.json').then(function (response) {
// fill up items with result, so next query just returns items.
for(var i=0;i<response.data.length;i++){
items.push(response.data[i]);
}
return items;
});
// Return the promise to the controller
return promise;
} …Run Code Online (Sandbox Code Playgroud) 我想在角度应用程序中使用$ http.get尝试使用Web API GET控制器,如下所示:
$http.get(BasePath + '/api/documentapi/GetDocuments/' ,
{
params: {
PrimaryID: ID1,
AlternateID: ID2,
}
}).then( ...
Run Code Online (Sandbox Code Playgroud)
在我的情况下,PrimaryID或AlternateID将具有该值.因此,其中一个将始终为null.
我的Web api方法是
public DocumentsDto[] GetDocuments(decimal? PrimaryID, decimal? AlternateID)
{ ...
Run Code Online (Sandbox Code Playgroud)
当其中一个值为null时,$ http.get生成的url如下所示:
http://BaseServerPath/api/documentapi/GetDocuments/?PrimaryID=1688
Run Code Online (Sandbox Code Playgroud)
要么
http://BaseServerPath/api/documentapi/GetDocuments/?AlternateID=154
Run Code Online (Sandbox Code Playgroud)
这没有达到我的Web API方法.
但是,如果我使用
http://BaseServerPath/api/documentapi/GetDocuments/?PrimaryID=1688&AlternateID=null
Run Code Online (Sandbox Code Playgroud)
有用.我可以在我的参数中将值硬编码为null,但是我想知道是否有任何正确的方法来实现这一点.
谢谢,山姆
当我将$ http设置为缓存请求时,我仍然看到从浏览器网络发送到服务器的重复请求(具有相同的URL和相同的数据),
$http.post(url, data, {cache:true} ).success(function(response) {
Run Code Online (Sandbox Code Playgroud)
我有以下问题:
我是angular2的新手,我花了很多时间找到解决方案,但我没有.我想从一个类中的http调用存储转换一个oberservable.
我有以下内容:
json文件
[{
"nickname": "magicMike",
"id": "123",
"name": "Michael",
"nachname": "Fischer",
"pictURL": "../images/mainPanel/Image_dummy.jpg",
"city": "Ulm"
}]
Run Code Online (Sandbox Code Playgroud)
用户类文件:
export class User {
nickname: string;
id: string;
name: string;
nachname: string;
pictURL: string;
city: string;
constructor(
nickname: string,
id: string,
name: string,
nachname: string,
pictURL: string,
city: string
){
this.nickname = nickname;
this.id = id;
this.name = name;
this.nachname = nachname;
this.pictURL = pictURL;
this.city = city;
}
}
Run Code Online (Sandbox Code Playgroud)
读取json文件的服务
import { Component, Input } from '@angular/core';
import { Injectable } …Run Code Online (Sandbox Code Playgroud) 我有一个html结构,组件内部有一个组件(忘记了正确的单词).
基本上像这样工作(很大程度上简化):
主要的HTML:
<div *ngFor="let item of data">
<app-item [item]="item"></app-item>
</div>
<button (click)="addItem()">Add</button>
Run Code Online (Sandbox Code Playgroud)
item html:
<div>{{item.name}}</div>
<button (click)="deleteItem()">Delete</button>
Run Code Online (Sandbox Code Playgroud)
在app-item中我从列表中显示了几个项目.该列表通过http.get请求将数据直接从数据库中获取.
现在我还有添加或删除两个工作项的功能(项目分别添加到数据库或从数据库中删除项目就好了).虽然更改检测不会选择其中任何一个并且需要刷新站点(例如通过F5)来显示更改.
我检查了代码(并非所有代码都来自我)并且找不到任何指定的更改检测策略.
我还尝试通过ChangeDetectorRef(this.ref.detectChanges();)从添加和删除功能手动触发更改检测.虽然这也没有消除手动刷新页面以查看更改的需要.
现在我发现变量检测缺少什么呢?或者,我如何在添加/删除方法中手动触发它?
在引入angular的新HttpClient之前,可以使用instanceof关键字验证从http api调用返回的对象.他们不再能够使用HttpClient模块了.我正在尝试一些简单的方法,但类型检查每次都返回false.期望的行为:
```
getCow() {
return this.http.get<Cow>(ApiRoute.GET_COW, options)
.map(res => res as Cow)
.toPromise()
.then((c: Cow) => {
console.log(c instanceof Cow); //this is false
})
}
Run Code Online (Sandbox Code Playgroud)
```
会回归真实.有谁知道在http客户端幕后新建一个实例的简单方法?
$http和之间有什么区别$q?$q实施$http,反之亦然?$http,并$q在同一时间?不幸的是,我们的运行速度为1.2.26(当它被宝石化时会升级到1.2.28).
在此期间,我如何修补(heh)$ http以便可以使用简写patch方法?我对整个服务/工厂/模块的事情都很陌生.我已经做了几个小时的搜索,似乎无法弄明白.
myApp.factory('patchedHTTP', function($http, BasicService) {
// $http['patch'] = function(url, data, config) {
// return $http(angular.extend(config || {}, {
// method: 'patch',
// url: url,
// data: data
// }));
// };
var extended = angular.extend(BasicService, {});
extended.createShortMethodsWithData('patch');
return extended;
});
Run Code Online (Sandbox Code Playgroud)
以上是我得到的最好的......它没有做任何XD
我希望每个人都做得很好.我最近开始使用angular 4.4,我一直试图将数据发布到我的api服务器,但不幸的是它没有用.我花了两天时间,但仍然没有成功.并且已经尝试过6-7篇甚至来自angular.io的文章.我已经尝试了Http和Httpclient模块,但似乎没有任何工作.
问题是,每当我尝试将数据发布到我的服务器时,Angular都会生成http OPTIONS类型请求而不是 POST.
this.http.post('http://myapiserver.com', {email: 'adam@example.com'}).subscribe(
res => {
const response = res.text();
}
);
Run Code Online (Sandbox Code Playgroud)
我也试图发送请求的自定义选项,但仍然没有成功.
const headers = new Headers({ 'Content-Type': 'x-www-form-urlencoded' });
const options = new RequestOptions({ headers: headers });
options.method = RequestMethod.Post;
options.body = {name: 'Adam Smith'};
//options.body = JSON.stringify({name: 'Adam Smith'}); // i also tried this
//options.body = 'param1=something¶m2=somethingelse'; // i also tried this
Run Code Online (Sandbox Code Playgroud)
我使用的是ASP.NET核心2.0,但由于它不起作用我也试过简单的PHP代码,这里是php的服务器端代码.它也没有用.
<?php
print_r($_POST);
?>
Run Code Online (Sandbox Code Playgroud)
注意:Cors也在服务器上启用.另外,我也试过简单的get请求,它的工作非常好.
我真的很感激一些帮助.
提前致谢
javascript http-post angular-http angular angular-httpclient
我正在使用 Angular 4。还为任何 http 请求使用了 http 拦截器。https://angular.io/api/common/http/HttpInterceptor
但我使用 xmlHttpRequest 的发布请求之一用于某些文件上传事件。标头在 http post 请求(this.http.post(...))中设置正确。但是在 xmlHttpRequest 中,没有调用拦截器。所以不能设置标题。我如何处理 xmlHttpRequest(post 方法)中的 http 拦截器?
angular-http ×10
angular ×5
angularjs ×5
typescript ×2
http-post ×1
interceptor ×1
javascript ×1