fetch-api 相关问题

Fetch API https://fetch.spec.whatwg.org是XHR的改进替代品,用于制作异步HTTP请求,同时更好地管理重定向以及与CORS和服务工作者的交互。


javascript`fetch()`不会在django

在Django的框架中遇到了问题。相关的代码线如下所述。 这些是HTML线,其中第一个DIV和嵌套的P将充满其他HTML El ...

回答 1 投票 0


获取功能不发送cookie

import React from 'react' import { lapi, api } from './api' const __test__ = () => { const send = async () => { try { const res = await fetch(`${api}/api/v1/user/profile`, { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/json' } }); const data = await res.json(); if (data.status === 200) { console.log("OK"); console.log(data); } else { console.log("None"); } } catch (error) { console.error("Error:", error); } }

回答 1 投票 0

我正在尝试从API中获取数据,但是它引发了一个错误,说:

uncaugh运行时错误: data.map不是函数 TypeError:data.map不是函数

回答 1 投票 0

不可能从公共目录获取和显示HTML内容

需要帮助Vite + React,其中一个人无法从公共目录中获取和显示内容。尽管遵循了各种故障排除步骤,但项目的内容home.html是...

回答 0 投票 0





取出API不会让我发布JSON数据

我正在尝试将JSON对象发送到我的控制器以保存新类别。这是我的表格,底部有按钮: <div id="register_modal" class="modal"> <div class="row"> <form id="registration" class="col s12 m12 l6" method="post"> @Html.AntiForgeryToken() <div class="modal-content dark-text"> <div class="row"> <h2 class="modal_heading">Join ButtaLove</h2> <div class="input-field col s12 m4"> <input id="Name" name="First" placeholder="Name" type="text" class="validate"> <label for="Name"><i class="material-icons left">face</i>Name</label> </div> <div class="input-field col s12 m4"> <input id="Description" name="Last" placeholder="Description" type="text" class="validate"> <label for="Description"><i class="material-icons left">face</i>Description</label> </div> </div> <div class="modal-footer"> <button data-action-link="@Url.ActionLink("AddCategory", "Users")" onclick="Main.AddCategory(event)" id="register_sendit" class="btn waves-effect waves-light" type="button"> Join <i class="material-icons right">send</i> </button> </div> </div> </form> </div> </div> 我的javascript方法运行,而获取永远不会击中服务器。我已经设置了内容类型并接受标题为应用程序/json: Main.AddCategory = (event) => { const form = event.currentTarget.closest('form'); const name = form.querySelector('#Name').value.trim(); const desc = form.querySelector('#Description').value.trim(); //antiforgery request token const token = document.getElementsByName("__RequestVerificationToken")[0]; const actionLink = event.currentTarget.dataset.actionLink; const response = fetch(actionLink, { method: "POST", headers: { "Accept": "application/json", "Content-Type": "application/json", "MY-XSRF-TOKEN": token.value }, referrer: "about:client", referrerPolicy: "same-origin", //mode: 'same-origin', //credentials: 'same-origin', cache: "no-cache", //keepalive: true, //redirect: 'manual', body: JSON.stringify({ Name: name, Description: desc }) }); if (!response.ok) { throw new Error(`Error saving new category.. `); } }; 我正在使用.NET 9,而我的控制器操作则期待帖子,其中cantorydto对象参数: [ValidateAntiForgeryToken] [HttpPost] public async Task<JsonResult> AddCategory([FromBody]CategoryDTO categoryDTO) { if (!ModelState.IsValid) { return Json(new { Success = false, Message = "Failure creating category: " + categoryDTO.Name }, System.Web.Mvc.JsonRequestBehavior.AllowGet); } Category newCategory = new() { Name = categoryDTO.Name, Description = categoryDTO.Description }; await _db.Categories.AddAsync(newCategory); await _db.SaveChangesAsync(); return Json(new { Success = true, Message = $"The category, {categoryDTO.Name} was created successfully." }, System.Web.Mvc.JsonRequestBehavior.AllowGet); } 我的DTO课程: public class CategoryDTO { public string? Name { get; set; } public string? ProductId { get; set; } public string? Description { get; set; } } 我不知道我是否需要尝试将数据作为另一种内容类型或什么,我变得绝望...显然我希望能够在提取请求中发送JSON数据...或者也许我也许我应该返回使用jquery.ajax()? 我知道这与我在线阅读的内容类型有关,但没有 我尝试过解决这个问题...这也不是CORS问题...所有内容都设置为相同的来源...我还启用了CORS并告诉我的应用程序以允许我的特定来源: app.UseCors(policy => { policy.AllowAnyHeader().AllowAnyMethod().WithOrigins(["https://localhost:44319", "https://localhost:44369", "https://buttalovemantequilla-amor.com"]); }); app.UseAuthentication(); app.UseAuthorization(); 我今天必须检查所有这些,但我最终使用了。 formdata()提交表单值...它确实有效,但更多的是解决方法。.我仍然需要找出JSON: Main.Register = (event) => { let registerForm = event.currentTarget.closest('form'); let registerFormData = new FormData(registerForm); console.log([...registerFormData]); fetch(window.location.origin + '/Users/Register', { method: 'POST', cache: "no-cache", body: registerFormData }).then(res => res.json()) .then(resJSON => console.log(resJSON)) .catch(err => console.error(err)); };

回答 1 投票 0

脚本中的逻辑上的JS提取请求

我尝试使用React本机脚本中的fetch函数从服务器数据库(用烧瓶运行)检索数据。 我将获取和恢复分为函数中的JS对象。数据是

回答 1 投票 0


以使用加载状态停止未定义数据

因此,我知道在setState呼叫中使用的数据要等到下一个渲染,并且已经查找很多以弄清楚该怎么办。我一直发现的最好的解决方案是使用l ...

回答 1 投票 0

进行使用加载状态以停止未定义的数据反应

因此,我知道在setState呼叫中使用的数据要等到下一个渲染,并且已经查找很多以弄清楚该怎么办。我一直发现的最好的解决方案是使用l ...

回答 1 投票 0




中止http请求的摩擦是什么[关闭]

我多次看到了一个流产的人来流产Axios请求,这是什么意思?为什么如此重要? 这是我在使用效果钩中发现的一个示例: apcontrollerref.current?

回答 1 投票 0


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.