OJ前端对接口

OJ前端对接口


原文链接

1.导入注册接口

①新建接口js文件(与接口名一致)

请添加图片描述

②首先导入post或者get

import { post } from "utils/http";

③导出接口名

export default{
 	
}

④接口命名—接口的最后名字

例如在下面的例子中,就选用objectiveJudge

请添加图片描述

⑤选择请求方式,填写路径

请添加图片描述

⑥完整接口导入代码

import { post } from "utils/http";
// 客观题判题
export default{
    objectiveJudge:post('/objective/objectiveJudge')
}

⑦在index.js中注册

导入

请添加图片描述

导出

请添加图片描述

2.调用接口

完整代码

import api from 'api'
export default {
  data() {
    return {
      lastContent: [
        {
          id: 0,
          topicNum: '6-1',
          topicContent: '将字符串变为html可使用()、()方法实现',
          blankNum: 2,
          TorF: true,
          allScore: '8分',

          blankAll: [
            // { preAnswer: '第一空答案:', blank: '', score: '2分' },
            // { preAnswer: '第二空答案:', blank: '', score: '6分' },
            {
              qtopicsetId: 306,
              userAnswer: [
                {
                  // preAnswer: '第一空答案:',
                  blank: '',
                  // score: '2分',
                },
                {
                  // preAnswer: '第二空答案:',
                  blank: '',
                  // score: '6分',
                },
              ],
            },
          ],
        },
      ],
    }
  },

  methods: {
    async submit() {
      const data = await api.objectiveJudge.objectiveJudge({
        accountId: 28,
        topicsetId: 1,
        userAnswer: this.lastContent[0].blankAll,
        topicTypeid: 4,
      })

      console.log(data)
    },
  },

①导入api

import api from 'api';

②在methods中添加异步方法async

async submit(){
      
}

③用constawait接口名

const data = await api.objectiveJudge.objectiveJudge({
        
 })

④填写请求数据

请求数据为后端需要的数据

请添加图片描述

const data = await api.objectiveJudge.objectiveJudge({
        accountId:28,
        topicsetId:1,
        userAnswer:this.lastContent[0].blankAll,
        topicTypeId:4,
      })

⑤使用data

console.log(data);

3.使用接口

①设置一个按钮,绑定点击响应事件submit(刚才设置异步方法的名字)

    <el-button type="success" @click="submit">提交</el-button>

②查看是否完成

右键检查,打开netWork,点击Fetch/XHR查看
出现以下画面及调用成功
在这里插入图片描述


接下来就是漫长的和后端拉扯的过程……

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值