ASP.NET core webapi js请求 _平台:windows (6)

版权声明:版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/qq_36051316/article/details/85063021

ASP.NET core webapi js请求 _平台:windows (6)

不懂怎么搞wwwroot的看上一篇文章:https://blog.csdn.net/qq_36051316/article/details/85062463

在我们首页加上一个js代码:


<script>
    //JS请求
    var request = new XMLHttpRequest();
    request.open("GET","http://localhost:30000/api/values/get",true);
    request.send();
    request.onreadystatechange = function(){
        if (request.readyState==4 && request.status==200) {
            console.log(request.responseText)
        }
    }
</script>

代码如下图:

html代码

我们ValuesController 里面的 get方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace mynetcorewebapi.Controllers
{
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [RouteAttribute("api/values/get")]
        [HttpGet]
        public object Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
}

代码如下图:

Controllers的代码

结果如下:

得到后端的一结果

猜你喜欢

转载自blog.csdn.net/qq_36051316/article/details/85063021