我想使用模型并发送到服务器。
例如,发送评论。
评论.model.ts
export interface Comment {
article_no: number;
username: string;
nickname: string;
creatat: Date;
content: string;
}
Run Code Online (Sandbox Code Playgroud)
评论.service.ts
import { Injectable } from '@angular/core';
import { ApiService } from '.';
import { Observable } from 'rxjs/Observable';
import { Comment } from '..';
@Injectable()
export class CommentService {
constructor(
private apiService: ApiService
) { }
public writeComment(comment: Comment) {
return this.apiService.post('/api/comment/', comment);
}
}
Run Code Online (Sandbox Code Playgroud)
comment.component.html
(我使用材质 UI)
<form class="comment-form">
<mat-form-field class="example-full-width">
<textarea matInput matTextareaAutosize [formControl]="content" placeholder="comment"></textarea>
</mat-form-field>
<button class="ui primary …Run Code Online (Sandbox Code Playgroud) 我想将字符串转换为Json并给它一个返回值。
c.JSON(200, string(body))通过 POST 请求(writePost)接收到的值是:
"{\"消息\":{\"@type\":\"响应\",\"@service\":\"service.community.cafe\",\"@version\":\"1.0. 0\",\"状态\":\"200\",\"结果\":{\"msg\":\"成功\",\"url\":\"aaabcd\",\"articleId \":211,\"articleUrl\":\" https://abcde.com/abc/211 \"}}}"
// WriteResult Struct
type WriteResult struct {
Message int `form:"msg" json:"msg"`
URL string `form:"url" json:"url"`
ArticleID int `form:"articleId" json:"articleId"`
ArticleURL string `form:"articleUrl" json:"articleUrl"`
}
func writePost(c *gin.Context) {
var writeInfo WriteInfo
if err := c.ShouldBind(&writeInfo); err != nil {
fmt.Println("error : ", err)
}
url := "https://openapi.abcde.com/articles"
var bearer = "Bearer " + writeInfo.AccessToken
var bufs bytes.Buffer
form := url.Values{}
form.Add("subject", subject)
form.Add("content", content)
req, err := http.NewRequest("POST", url, …Run Code Online (Sandbox Code Playgroud)