ASP.NET core webapi jquery请求 _平台:windows (7)

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

ASP.NET core webapi jquery请求 _平台:windows (7)

下载jquery 3.3.1
http://www.daolizhe.xyz/web-front-frame/jquery/3.3.1/jquery-3.3.1.min.js

其他操作还是和我们的 上一篇文章相同:就是我们的js代码换成了jq代码,不懂环节的直接看上一篇文章:

ASP.NET core webapi js请求 _平台:windows (6)
https://blog.csdn.net/qq_36051316/article/details/85063021

把script 里面的东西换成下面代码:

最后面有完整的前端代码和完整控制器代码 点我跳转代码

    // JQuery请求
    $.ajax({
        url: "http://localhost:30000/api/values/get",
        type: "GET",
        async: true,
        beforeSend: function (obj) {
            // 在发送请求之前执行此函数
            // 如果return false 则取消发送
            console.log("欢迎请求!下面输出对象")
            console.log(obj)
            // return false;
        },
        timeout: 5000,
        cache: true,
        success: function (res) {
            console.log(res);
        }
    });

如下图所示:

jq代码展示图

控制器里面的代码还是不变:

控制器的代码.
运行结果:

运行结果

前端:

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <script src="jquery-3.3.1.min.js"></script>
</head>

<body>
    我是首页
</body>

</html>
<script>
    // JQuery请求
    $.ajax({
        url: "http://localhost:30000/api/values/get",
        type: "GET",
        async: true,
        beforeSend: function (obj) {
            // 在发送请求之前执行此函数
            // 如果return false 则取消发送
            console.log("欢迎请求!下面输出对象")
            console.log(obj)
            // return false;
        },
        timeout: 5000,
        cache: true,
        success: function (res) {
            console.log(res);
        }
    });

</script>

后端:

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" };
        }
    }
}

猜你喜欢

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