我使用AngularJs,我需要在$ http.post连接后返回一个数据.这是代码:
app.controller('PrincipalController',['$http', '$scope', function($http,$scope){
$scope.myData = 0;
$http.post("server/checklogin.php",
{id: "id",email: "email",password: "password"}
)
.success(function(data){
console.log($scope.myData) //0
$scope.myData = data;
console.log($scope.myData); // 1
}
Run Code Online (Sandbox Code Playgroud)
OLD:如何将数据传递给myData? - 感谢您的回答,对不起,如果我没有解释清楚.
但现在,在HTML中,{{myData}}我能写些什么?
我不知道它是否是基础,但我必须使用"ng-if"来使用"myData".
编辑:谢谢!我想我已经解决了!:d
我的$http函数可以返回以下错误:
POST http://foobar.dev/foobar 500(内部服务器错误)
POST http://foobar.dev/foobar 401(未经授权)
有没有办法可以捕获所有状态代码?
$http.post('/foobar', form)
.success(function(data, status, headers, config) {
console.info(data);
})
.error(function(data, status, headers, config) {
console.error(data);
if(status === 401) {
$scope.setTemplate('show-login');
}
if(status === 500) {
$scope.setTemplate('server-error');
}
}
);
Run Code Online (Sandbox Code Playgroud)
$scope.setTemplate()Controller中设置视图的函数在哪里.
但是我必须为每个error()函数执行此操作,并且有很多这样的函数也没有使它成为DRY代码:P
我想要的是捕获错误并根据错误中返回的状态代码执行操作.
仅供参考:我没有使用Angulars $routeProvider().
我正在开发示例应用程序来学习angularjs使用node.js. 当我将数据发布到后端以创建新数据时,family我收到错误:
Error: $http:badreq
Bad Request Configuration
Http request configuration url must be a string. Received:
{
"method":"POST",
"url":"api/family",
"data": {
"username":"fagruddin",
"password":"valaanur",
"familyLeader":"fagruddin",
"husband":"fagruddin",
"wife":"rejiya",
"child":2
},
"headers":{
"Content-Type":"application/x-www-form-urlencoded"
}
}
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题?有人帮我解决这个问题吗?
我正在尝试向我的有角js GET请求中添加自定义标头,如下所示:
$http({
method : 'GET',
url : s,
headers : {
"partnerId" : 221,
"partnerKey" : "heeHBcntCKZwVsQo"
}
})
Run Code Online (Sandbox Code Playgroud)
但是问题是标题被添加到如下所示的Access-Control-Request-Headers中,并且我收到403禁止响应:
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:50.0)
Gecko/20100101 Firefox/50.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/ *;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: GET
Access-Control-Request-Headers: partnerid,partnerkey
Origin: http://localhost:8080
DNT: 1
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)
我也尝试了以下更改,但没有运气
return $http.get(s, {
headers : {
'partnerId' : 221,
'partnerKey': 'heeHBcntCKZwVsQo'
}
})
Run Code Online (Sandbox Code Playgroud)
在SO的其他相关答案中,我看到需要在服务器端启用标头partnerId和partnerKey。但是我能够在POSTMAN客户端和其他POST客户端中添加这些自定义标头,并能够获得预期的响应。所以我想我缺少了一些东西。有人可以在这方面指导我。提前致谢
编辑:我注意到的另一件事是,在传递请求时,partnerId被替换为partnerid。不知道这是否有所作为。
我正在尝试在我的Http服务中构建一个无限轮询,因为我正在建立一个dashborad之王来调查来自服务器的数据,这里是我的代码几乎正在工作(在我的控制台中,我看到Json正在进行,但它没有'反思我的看法...我想注入我的用户:可观察
const usersURL = 'http://my.super.servor.php'
@Injectable()
export class UserService {
users: Observable<User[]>
constructor (public http:Http) {
this.users = http.get(usersURL)
genre mobile ou autre
.map(res => [res.json()]);
let i = this.users.subscribe(
usersURL => console.log(usersURL),
() => {}, // Here we catch up errors
() => console.log("completed!") // Here we catch up if its completed
)
// Here's where I'm trying to do the polling every 5 secondes
let tick$ = Observable.interval(5000);
let response$ =
tick$
.flatMap(() => http.get(usersURL))
.map(res => [res.json()]); …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我有一个User对象,该对象可以具有与之关联的可变数量的帐户 ID。当用户更新时,应为用户对象中包含的每个帐户发出额外请求:
class User {
id: number;
accountIds: Array<number>;
}
// Side effects class
@Effect()
update$ = this.actions$
.ofType('USER_UPDATE')
.switchMap(action => {
let user = action.payload;
for (let accountId of user.accountIds) {
let account = { id: accountId, userIds: [user.id] };
// Is there any way to chain these api calls with switch maps
// and then finally switch map to the user request below?
return this.apiService.post(`/accounts/${account.id}`, account);
}
return this.apiService.post(`/users/${userid}`, user);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用 RxJS 使用switchMap(或类似的东西)将这些调用链接在一起,以便在 …
我Http之前使用的是Angular 模块,并且该方法res.json()用于正常工作.我最近尝试过,HttpClient但res.json()似乎没有工作.只有使用res作品才能告诉我http客户端发生了什么变化.
return this.client.get('https://swapi.co/api/people/1/')
.map((res:Response) => {
return res.json(); // using maps to filter data returned form the http call this json dosn't work with http client
}).map(data => {
return data; // using maps of maps to filter data returned form the map
}).flatMap((jedi) => this.http.get(jedi['homeworld'])
.map(res => {
return res.json().name; // using flat maps to combine data returned from two observables into one
}).catch((error:any) => Observable.throw(error.json().error || 'Server …Run Code Online (Sandbox Code Playgroud) 目前我的代码是通过在彼此内部嵌套 observables 来实现的...
makeRequest() {
return this.http.get().map((response: Response) => {
return this.http.get(response.doSomething());
}
}
Run Code Online (Sandbox Code Playgroud)
当我需要使用它时,我也使用嵌套订阅它们......
我很确定这不是正确的方法,现在我需要一种方法来做一些事情:
-> 发出请求 -> 检查回复 -> 如果回复表明令牌已过期 -> 获取新令牌并再次执行请求 -> 否则将结果传递
实现这一目标的推荐方法是什么?
非常感谢,
以前我使用过
import { Http, Response, Headers, URLSearchParams } from "@angular/http";
Run Code Online (Sandbox Code Playgroud)
对于 API 调用
getprojectscount(city, param){
let urlSearchParams = new URLSearchParams();
urlSearchParams.set('limit', param.limit );
urlSearchParams.set('limitrows', param.limitrows );
urlSearchParams.set('locality', param.locality );
return this.http
.get(this.myapiurl + city + "?", { search: urlSearchParams })
.pipe(map(response => response.json().Counts));
}
Run Code Online (Sandbox Code Playgroud)
在此 URLSEARCHPARAMS 方法中,它运行良好。-> 因为当我们传递参数时。仅当需要时,它才会传递给 urlSearchParams 。
目前正在使用
import { HttpErrorResponse, HttpParams } from '@angular/common/http';
Run Code Online (Sandbox Code Playgroud)
AND 来到 HttpParams。当我使用这个 HTTPPARAMS 时,每次每个参数都通过 api 传递,如果它也为 null。
对于 API 调用
getprojectcount(city,param){
let params = new HttpParams();
params = params.append('limit', param.limit);
params …Run Code Online (Sandbox Code Playgroud) 我创建了一个jsfiddle,当我点击测试按钮时,我正在使用$ http执行http get,$ http.get的语法和结构对我来说似乎是正确的,但它不是发送选定的参数(查询) url中的字符串{data:$ scope.newuser}).
这是小提琴:
jsfiddle.net/8sZLs/2/
在PHP方面,我试过,但没有显示发布的值
angular-http ×10
angular ×5
angularjs ×5
javascript ×3
rxjs ×2
angular9 ×1
cors ×1
long-polling ×1
php ×1
rxjs5 ×1