🔥یلدا با راکت با (( ۷۰ درصد )) تخفیف! یلدا طولانی است، اما این تخفیف نه.
مشاهده دورههابا سلام
من یک پروژه در vs 2022 از نوع asp core with react ایجاد کردم .
از axios برای ارتباز با کنترلر استفاده کرده ام.
function loginUser(dispatch, login, password, history, setIsLoading, setError) {
setError(false);
setIsLoading(true);
if (!!login && !!password) {
alert('s');
setError(null)
setIsLoading(false)
dispatch({ type: 'LOGIN_SUCCESS' })
try {
axios.post("api/Account/Login", {
User: login,
Pass: password
}).then(result => {
console.log(result);
});
}
catch (error) {
if (error.response) {
console.log(error.response);
} else if (error.request) {
console.log(error.request);
} else if (error.message) {
console.log(error.message);
}
}
} else {
dispatch({ type: "LOGIN_FAILURE" });
setError(true);
setIsLoading(false);
}
}
و در کنترلر
I am using React on the front-end and need to request some data from a server.
I use axios in react for post data to controller.
function loginUser(dispatch, login, password, history, setIsLoading, setError) {
setError(false);
setIsLoading(true);
if (!!login && !!password) {
alert('s');
setError(null)
setIsLoading(false)
dispatch({ type: 'LOGIN_SUCCESS' })
try {
axios.post("api/Account/Login", {
User: login,
Pass: password
}).then(result => {
console.log(result);
});
}
catch (error) {
if (error.response) {
console.log(error.response);
} else if (error.request) {
console.log(error.request);
} else if (error.message) {
console.log(error.message);
}
}
} else {
dispatch({ type: "LOGIN_FAILURE" });
setError(true);
setIsLoading(false);
}
}
and in controller
[ApiController]
[Route("api/{controller}")]
public class AccountController : ControllerBase
{
private readonly IApplicationUserManager _userManager;
private readonly IApplicationRoleManager _roleManager;
private readonly SignInManager<ApplicationUser> _signInManager;
public AccountController(IApplicationUserManager userManager, IApplicationRoleManager roleManager,
SignInManager<ApplicationUser> signInManager)
{
_userManager = userManager;
_roleManager = roleManager;
_signInManager = signInManager;
}
[HttpPost]
public IActionResult Login([FromBody] LoginViewModel model)
{
return Ok(100);
}
}
و در program
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html"); ;
app.Run();
بعد از اجرای برنامه متد راخوانی نمی شود.
من در کدهای شما ندیدم پکیج axios رو ایمپورت کرده باشید
از نصب بودن پکیج و ایمپورت شدنش در اول فایلتون مطمئن بشید
import axios from 'axios'
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟