一、前段HTML
1、引入jquery文件:https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js

2、編寫js事件,事件觸發時調用該ajax請求。
2.1、jquery封裝了一個ajax的方法。參數是一個對象。這個對象包含以下屬性:
請求地址:URL: 'http://IP地址/端口/接口地址'
請求方式:type:'get/post/put/delete'
請求數據類型:datatype: 'json'
請求數據:data: '{uname: "zhangsan", pwd:'123456'}'
請求回調函數:success function(data){}, error function(data){}

以上代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<input type="text" id="name"/>
<input type="text" id="age"/>
<input type="button" id="btnCommit" onclick="ajaxFunc()" value="button"/>
<input type="button" id="btnClear" value="Clear">
</body>
<script>
function ajaxFunc() {
$.ajax({
url: "http://localhost:53179/api/values/post",
type: 'post',
dataType: 'json',
data: {id:22},
success: function (res) {
console.log(res)
if(res!=null){
$('#name').attr('value',res.username);
$('#age').attr('value',res.age);
}
},
error function(res){
console.log(res);
},
complete function(status){
console.log(res);
}
})
};
$('#btnClear').on('click',function(){
$('#name').val("");
$('#age').val("");
});
</script>
</html>
后端net core web api
1、創建一個工程項目之后,添加一個控制器繼承Controller
1.1編寫接口(這里沒有使用數據庫),主要是測試前端請求之后能否執行調用該接口

2、調試接口正常,但是會發現,前段調用的時候會出現跨域問題。
解決如下:在startup類配置跨域

services.AddCors(options => options.AddPolicy("DomainKYHttp",
builder => builder.AllowAnyMethod()
.AllowAnyHeader()
.AllowAnyOrigin()
.AllowCredentials()));
最后運行后台代碼,使用瀏覽器打開第一步創建的html。測試成功

