Axios+SpringBoot實現GET、POST請求(例子爲markdown)

1. GET請求

前端:

	  axios
        .get('https://127.0.0.1:9000/index/getArticle', {
          params: {
            id: 1
          }
        })
        .then(res => {
          if (res.status == 200) {
            console.log('後臺返回的消息===>', res.data)
            this.html = res.data.html
            this.content = res.data.markdown
          } else {
            console.log(res)
          }
        })
        .catch(error => {
          console.log('網絡請求錯誤', error)
        })

後臺:

	@GetMapping("/getArticle")
    public String getArticle(Integer id){
        System.out.println(html+"=="+id+"==>"+markdown +"\n");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("html",html);
        jsonObject.put("markdown",markdown);
        return jsonObject.toString();
    }

2. POST請求

	  var params = new URLSearchParams()
      params.append('markdown', this.content)
      params.append('html', this.html)
      axios
        .post('https://127.0.0.1:9000/index/upArticle', params)
        .then(res => {
          if (res.status == 200) {
            console.log('===>', res)
          } else {
            console.log(res)
          }
        })
        .catch(error => {
          console.log('網絡請求錯誤', error)
        })
    @PostMapping("/upArticle")
    public String upArticle(@RequestParam("html") String html,
                            @RequestParam("markdown") String markdown){
        this.html = html;
        this.markdown = markdown;
        return html;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章