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

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

如下图所示:

ASP.NET core webapi jquery请求 _平台:windows (7)_第1张图片

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

ASP.NET core webapi jquery请求 _平台:windows (7)_第2张图片.
运行结果:

ASP.NET core webapi jquery请求 _平台:windows (7)_第3张图片

前端:





    
    
    
    首页
    



    我是首页




后端:

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

你可能感兴趣的:(dotnet,core,webapi)