相关代码目录结构
request.js
数据返回格式可参考:
若操作正常,状态码为0
import axios from 'axios'
const ERR_OK = 0
export function get(url){
return function(params){
return axios
.get(url,{
params
})
.then(res=>{
const{
errno,
data
} = res.data
if(errno===ERR_OK){
return data
}
})
.catch(e=>{
})
}
}
index.js
import {get} from './request'
const getData = get('https://localhost:7211/api/Test/ReturnData')
const getDataByNo = get('https://localhost:7211/api/Test/ReturnDataByNo')
export{
getData,
getDataByNo
}
login.vue
import {getData,getDataByNo} from '../api'
onMounted( ()=>{
// axios.get("https://localhost:7211/api/Test/ReturnData")
// .then(res=>{
// console.log(res)
// })
get()
getByNo(1)
} )
const get = ()=>{
getData().then(res=>{
console.log(res)
})
}
const getByNo = (no)=>{
getDataByNo({no})
.then(res=>{
console.log(res)
})
}
controller
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Xml.Linq;
namespace WebApplication4.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpGet]
public Object ReturnData()
{
return new {
errno = 0,
data = new
{
name = "wangwu",
age = 33
}
};
}
[HttpGet]
public Object ReturnDataByNo( int no)
{
if(no == 1)
{
return new
{
errno = 0,
data = new{
name = "zhangsan",
age = 22
}
};
}
else
{
return new
{
errno = 0,
data = new
{
name = "sili",
age = 44
}
};
}
}
}
}