angular post 方法无法正常工作
angular post method is not working properly
在这个 POST 方法中,我正在做 console.log( 'value:- '+ this.newCourses);
时发布数据
这里对象对象来了,这里我发布的值没有打印
{ new course data saved succcesfully ,
this.token ... [object Object] ,
value:- [object Object] }
postData(){
this.token = getToken();
this.details['title'] = this.form.getRawValue();
this.details['category'] = this.form.getRawValue();
this.details['length'] = this.form.getRawValue();
this.details['content'] = this.form.getRawValue();
this.details['product_name'] = getProduct()['name'];
this.details['updated'] = 'Monday';
console.log(this.details);
this.httpHeaders = new HttpHeaders({
"Authorization": "Bearer " + this.token});
this._httpClient.post('http://127.0.0.1:8000/api/products/add-course/',this.details, {headers: this.httpHeaders}).subscribe(
result => {
console.log(result);
console.log("new course data saved succcesfully");
this.token = getToken();
this.newCourses = result;
console.log( 'value:- '+ this.newCourses);
},
error => {
console.log(error);
}
);
}
您刚刚看到的是将对象连接到字符串的结果。
如果要记录对象本身,将字符串和对象分开记录:
console.log('value:- ', this.newCourses);
注意我在这里将 2 个参数传递给 console.log
。
当您连接对象和字符串时,将调用对象的 toString()
方法。默认情况下,这将显示 [object Object]
.
const obj = { a: 1, b: 2, c: 3 };
// concatenate obj with string
console.log('value: - ' + obj);
// log string and object itself as separate args
console.log('value: - ', obj);
// log obj.toString()
const objString = obj.toString();
console.log(objString);
奖金编辑:
如果需要,您可以覆盖 toString()
:
// override toString()
const obj = { a: 1, b: 2, c: 3, toString: () => 'World!' };
console.log('Hello, ' + obj);
在这个 POST 方法中,我正在做 console.log( 'value:- '+ this.newCourses);
时发布数据
这里对象对象来了,这里我发布的值没有打印
{ new course data saved succcesfully ,
this.token ... [object Object] ,
value:- [object Object] }
postData(){
this.token = getToken();
this.details['title'] = this.form.getRawValue();
this.details['category'] = this.form.getRawValue();
this.details['length'] = this.form.getRawValue();
this.details['content'] = this.form.getRawValue();
this.details['product_name'] = getProduct()['name'];
this.details['updated'] = 'Monday';
console.log(this.details);
this.httpHeaders = new HttpHeaders({
"Authorization": "Bearer " + this.token});
this._httpClient.post('http://127.0.0.1:8000/api/products/add-course/',this.details, {headers: this.httpHeaders}).subscribe(
result => {
console.log(result);
console.log("new course data saved succcesfully");
this.token = getToken();
this.newCourses = result;
console.log( 'value:- '+ this.newCourses);
},
error => {
console.log(error);
}
);
}
您刚刚看到的是将对象连接到字符串的结果。
如果要记录对象本身,将字符串和对象分开记录:
console.log('value:- ', this.newCourses);
注意我在这里将 2 个参数传递给 console.log
。
当您连接对象和字符串时,将调用对象的 toString()
方法。默认情况下,这将显示 [object Object]
.
const obj = { a: 1, b: 2, c: 3 };
// concatenate obj with string
console.log('value: - ' + obj);
// log string and object itself as separate args
console.log('value: - ', obj);
// log obj.toString()
const objString = obj.toString();
console.log(objString);
奖金编辑:
如果需要,您可以覆盖 toString()
:
// override toString()
const obj = { a: 1, b: 2, c: 3, toString: () => 'World!' };
console.log('Hello, ' + obj);