ASP.NET core webapi jquery

Download jquery 3.3.1 www.daolizhe.xyz/web-front-f…

The other operations are still the same as in our last article: our JS code is changed to JQ code, and those who do not understand the link directly read the article:

ASP.NET core webapi js request _ platform: Windows (6) blog.csdn.net/qq_36051316…

Replace the contents of script with the following code:

At the back is the complete front-end code and the complete controller codeClick me jump code

/ / JQuery request $. Ajax ({url: "http://localhost:30000/api/values/get", type: "GET", "async" : true, beforeSend: Function (obj) {// Perform this function before sending the request // Cancel sending console.log if return false (" Welcome! ") console.log(obj) // return false; }, timeout: 5000, cache: true, success: function (res) { console.log(res); }});Copy the code

As shown below:

The code inside the controller remains the same:

.

Running results:

Front end:

<! 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, </title> <script SRC ="jquery-3.3.1.min.js"></script> </head> <body> < script > / / JQuery request $. Ajax ({url: "http://localhost:30000/api/values/get", type: "GET", "async" : true, beforeSend: Function (obj) {// Perform this function before sending the request // Cancel sending console.log if return false (" Welcome! ") console.log(obj) // return false; }, timeout: 5000, cache: true, success: function (res) { console.log(res); }}); </script>Copy the code

The backend:

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" }; }}}Copy the code

Welcome to qq group communication: 704028989