18910140161

JavaScript--如何获取后端api的url,以便在HTML-Stack Overflow中的jquery中使用它

顺晟科技

2022-10-19 12:38:56

245

我正在写一个登录表单,我需要对用户名和密码进行身份验证。 表单是用dreamweaver html代码编写的。并且数据库在visual studio.NET核心代码中是本地的。我仍在试图找出使用$POST和$GET方法的方法,但我甚至不能正确地编写url。我正在使用swagger托管我的API。

以下是我的visual studio控制器代码:

using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using authentication.Models;
using authentication.Repository;
using Microsoft.AspNetCore.Http;

namespace Taskb.Controllers
{

    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private UsersRepository users = new UsersRepository();
        private int count = 0;

        [HttpGet]
        public ActionResult<IEnumerable<user>> GetAllUser()
        {
            return users.GetAllUsers();

        }

        [HttpPost]
        public ActionResult<user> CreateUser( user newUser)
        {
            foreach (user item in users.GetAllUsers())
            {

                if (newUser.username == item.username && newUser.password == item.password)
                {
                    count++;
                }

            }

            if (count == 0)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, "Username or password incorrect");
            }

            return Ok("Authentication Successfull");
        }

    }
 }

下面是我的html代码:

using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using authentication.Models;
using authentication.Repository;
using Microsoft.AspNetCore.Http;

namespace Taskb.Controllers
{

    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private UsersRepository users = new UsersRepository();
        private int count = 0;

        [HttpGet]
        public ActionResult<IEnumerable<user>> GetAllUser()
        {
            return users.GetAllUsers();

        }

        [HttpPost]
        public ActionResult<user> CreateUser( user newUser)
        {
            foreach (user item in users.GetAllUsers())
            {

                if (newUser.username == item.username && newUser.password == item.password)
                {
                    count++;
                }

            }

            if (count == 0)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, "Username or password incorrect");
            }

            return Ok("Authentication Successfull");
        }

    }
 }

提前感谢您的帮助。


顺晟科技:

post方法应用作输入参数,如下所示:

using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using authentication.Models;
using authentication.Repository;
using Microsoft.AspNetCore.Http;

namespace Taskb.Controllers
{

    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private UsersRepository users = new UsersRepository();
        private int count = 0;

        [HttpGet]
        public ActionResult<IEnumerable<user>> GetAllUser()
        {
            return users.GetAllUsers();

        }

        [HttpPost]
        public ActionResult<user> CreateUser( user newUser)
        {
            foreach (user item in users.GetAllUsers())
            {

                if (newUser.username == item.username && newUser.password == item.password)
                {
                    count++;
                }

            }

            if (count == 0)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, "Username or password incorrect");
            }

            return Ok("Authentication Successfull");
        }

    }
 }

您的ajax请求应该这样发送:

using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using authentication.Models;
using authentication.Repository;
using Microsoft.AspNetCore.Http;

namespace Taskb.Controllers
{

    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private UsersRepository users = new UsersRepository();
        private int count = 0;

        [HttpGet]
        public ActionResult<IEnumerable<user>> GetAllUser()
        {
            return users.GetAllUsers();

        }

        [HttpPost]
        public ActionResult<user> CreateUser( user newUser)
        {
            foreach (user item in users.GetAllUsers())
            {

                if (newUser.username == item.username && newUser.password == item.password)
                {
                    count++;
                }

            }

            if (count == 0)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, "Username or password incorrect");
            }

            return Ok("Authentication Successfull");
        }

    }
 }

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航