Javascript 不执行put调用的角度可观测
我正在开发我的第一个Angular应用程序,但在进行Javascript 不执行put调用的角度可观测,javascript,angular,Javascript,Angular,我正在开发我的第一个Angular应用程序,但在进行http.put调用时遇到问题。以下是我调用的函数: updateUser(columns, values) : Observable<boolean> | boolean { const headers: Headers = new Headers(); // Need to set content type headers.append('Content-Type', 'application/json; charse
http.put
调用时遇到问题。以下是我调用的函数:
updateUser(columns, values) : Observable<boolean> | boolean {
const headers: Headers = new Headers(); // Need to set content type
headers.append('Content-Type', 'application/json; charset=utf-8');
headers.append('Authorization', `Bearer ${this.authenticationService.token}`);
const options = new RequestOptions({ headers: headers });console.log('test service');
return this.http.put(`${API_URL}users/${this.authenticationService.userId}`, JSON.stringify({ columns: columns, values: values }) , options)
.map((response: Response) => {
console.log('test service1');return Observable.of(true);
})
.catch(e => {
console.log('test service2');return Observable.of(false);
});
对于http.get函数,我通常执行以下操作:
loadStudents(page: number, grade = []) {
if (grade.length != 0){
this.student_query_filter = { key:'grade_level',value:grade.join('||') };
} else {
this.student_query_filter = {};
}
this.studentService.getStudentsCount([{ key: 'last_name', value: this.student_search_filter },this.student_query_filter])
.subscribe(
total => this.total = total, //Assign returned student count to local property
err => { console.log(err); });
}
- 您希望将数据作为对象传递,而不是使用
JSON.stringify
- 您希望从map返回结果,而不是另一个可观察的结果。如果您确实希望返回不同的可观察对象,则应将
更改为map
switchMap
- 签名应
,因为这是您要返回的内容可见
/
)
- 您希望将数据作为对象传递,而不是使用
JSON.stringify
- 您希望从map返回结果,而不是另一个可观察的结果。如果您确实希望返回不同的可观察对象,则应将
更改为map
switchMap
- 签名应
,因为这是您要返回的内容可见
/
)
谢谢你的反馈。但它似乎仍在做同样的事情。当我调用该函数时,控制台只显示:
testservice,发送到:http://10.1.7.12:3000/api/users/58d2ae2c801c9f4bd4626ba7
当我检查开发人员工具时,它没有显示对URL的请求。因此,它进入了这个功能,但没有调用一些reason@MrV-1)我更新了控制台.log
调用以包含url,您能否验证它是否显示了您期望的url?2) 如果这是正确的,请验证是否定义了this.http
,可能是因为它没有正确注入。添加console.log('Http定义='+!!this.Http)代码>在您打电话之前。谢谢您的帮助。以下是输出:Http defined=true
测试服务,发送到:http://10.1.7.12:3000/api/users/58d2ae2c801c9f4bd4626ba7
@MrV-我还注意到一件事(注意选项):this.http.put(
${API\u URL}users/${this.authenticationService.userId},{columns:columns,values:values},{headers:headers})
成功了!谢谢@Igor。谢谢你的反馈。但它似乎仍在做同样的事情。当我调用该函数时,控制台只显示:testservice,发送到:http://10.1.7.12:3000/api/users/58d2ae2c801c9f4bd4626ba7
当我检查开发人员工具时,它没有显示对URL的请求。因此,它进入了这个功能,但没有调用一些reason@MrV-1)我更新了控制台.log
调用以包含url,您能否验证它是否显示了您期望的url?2) 如果这是正确的,请验证是否定义了this.http
,可能是因为它没有正确注入。添加console.log('Http定义='+!!this.Http)代码>在您打电话之前。谢谢您的帮助。以下是输出:Http defined=true
测试服务,发送到:http://10.1.7.12:3000/api/users/58d2ae2c801c9f4bd4626ba7
@MrV-我还注意到一件事(注意选项):this.http.put(
${API\u URL}users/${this.authenticationService.userId},{columns:columns,values:values},{headers:headers})
成功了!谢谢你@Igor。
loadStudents(page: number, grade = []) {
if (grade.length != 0){
this.student_query_filter = { key:'grade_level',value:grade.join('||') };
} else {
this.student_query_filter = {};
}
this.studentService.getStudentsCount([{ key: 'last_name', value: this.student_search_filter },this.student_query_filter])
.subscribe(
total => this.total = total, //Assign returned student count to local property
err => { console.log(err); });
}
updateUser(columns, values) : Observable<boolean> {
const headers: Headers = new Headers(); // Need to set content type
headers.append('Content-Type', 'application/json; charset=utf-8');
headers.append('Authorization', `Bearer ${this.authenticationService.token}`);
const options = new RequestOptions({ headers: headers });
console.log('test service, sending to: ' + `${API_URL}users/${this.authenticationService.userId}`);
return this.http.put(`${API_URL}users/${this.authenticationService.userId}`, { columns: columns, values: values }, options)
.map((response: Response) => {
console.log('test service1');
return true;
})
.catch(e => {
console.log('test service2');
return false;
});
}
this.http.put(`${API_URL}users/${this.authenticationService.userId}`, { columns: columns, values: values }, options)
.subscribe((response: Response) => {
console.log('test service1'); }
, e => {
console.log('test service2');
});