gpt4 book ai didi

javascript - 为什么我收到此请求的 415

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:05 26 4
gpt4 key购买 nike

我有一个 Controller ,

[Route("api/[controller]")]
public class ResponsesController : Controller
{
[HttpPost]
public void Data([FromBody] MyModel value)
{
var temp = value;
}
}

[JsonObject]
[Serializable]
public class MyModel
{
public string Url { get; set; }
}

我正在使用 chrome 开发者控制台像这样发帖,

function post_to_url(path, params, method) {
method = method || "post";

var form = document.createElement("form");

//Move the submit function to another variable
//so that it doesn't get overwritten.
form._submit_function_ = form.submit;

form.setAttribute("Content-Type", "application/json;charset=utf-8");
form.setAttribute("method", method);
form.setAttribute("action", path);

for(var key in params) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);

form.appendChild(hiddenField);
}

document.body.appendChild(form);
form._submit_function_(); //Call the renamed function.
}
var j = {"Url":"hello"};
post_to_url("http://localhost:63984/api/responses", j );

我收到的错误是 415,但我不确定出了什么问题,我实际上想接收 myModel[]

最佳答案

来自 MDN

In a POST request, resulting from an HTML form submission, the Content-Type of the request is specified by the enctype attribute on the element.

您应该从 post_to_url 中删除 form.setAttribute("Content-Type", "application/json;charset=utf8")。如果您要以这种方式提交数据,您可能必须将服务配置为接受 url 编码的表单数据。对于这么简单的事情,它可能开箱即用。

关于javascript - 为什么我收到此请求的 415,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47204967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com