【问题标题】:Bad Request error when calling from jquery ajax to API contrroller ASP.net Core从 jquery ajax 调用到 API 控制器 ASP.net Core 时出现错误的请求错误
【发布时间】:2020-05-05 00:04:29
【问题描述】:

我正在尝试通过 ajax 调用通过单击按钮从页面调用 api。问题是对于 GET & DELETE ,调用正常,但对于 POST 和 PUT,它在 firefox 调试器中显示错误的请求格式。

namespace WebApplication1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class JsonDataAPIController : ControllerBase
    {
        // GET: api/JsonDataAPI
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/JsonDataAPI/5
        [HttpGet("{id}", Name = "Get")]
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/JsonDataAPI
        [HttpPost]
        public void Post([FromBody] string value)
        {
             //check for value and do stuff
        }

        // PUT: api/JsonDataAPI/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}

html页面中的函数调用

function apicall() {
        $.ajax({
            url: 'api/JsonDataAPI',
            type: 'POST',
            data: {value:'1234'},
            success: function (data) {
                console.log(data);
                console.log("success");
            },
            error: function () {
                console.log("error");
            }
        }).done(function () {
             //console.log("ajax");
        })
    }

最终我想通过 ajax 发送 JSON 数据并在 API 中工作。我知道如果我不发送任何 contenttype 和 datatypein ajax 调用,那么它将自动分配。那么为什么这个字符串数据不起作用,我无法得到。为什么API中有ControllerBase?不应该是 APIController 吗?

此外,当我尝试使用 Postman 时,甚至没有调用 API。如果我尝试通过 Firefox 中的 url 访问 api,那么它可以访问 GET api,但是当在 POSTman 中使用相同的 URL 时,它会显示“无法获得任何响应”。我在设置项目时以及在邮递员中都没有启用任何身份验证,我正确设置了参数(比如没有身份验证),仍然来自邮递员的调用不起作用。 对不起,但我正在学习,因此有很多问题。 提前谢谢你。

【问题讨论】:

    标签: jquery ajax asp.net-core asp.net-web-api


    【解决方案1】:

    问题是对于 GET & DELETE ,调用正常,但对于 POST 和 PUT,它在 firefox 调试器中显示错误的请求格式。

    请尝试修改您的代码,如下所示。

    $.ajax({
        url: 'api/JsonDataAPI',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify('1234'),
        success: function (data) {
            console.log(data);
            console.log("success");
        },
        error: function () {
            console.log("error");
        }
    }).done(function () {
            //console.log("ajax");
    })
    

    【讨论】:

    • 它可以工作,但我不明白字符串数据类型如何接受以 json 格式发送的数据。你能解释一下吗?谢谢你的回答。
    【解决方案2】:

    它与 API 无关。由于您将字符串值作为 HTTP POST 传递,因此您需要像这样使用 javascript 代码。

    $.ajax(
    {
        url: "/api/JsonDataAPI",
        type: "POST",
        dataType: 'json',
        data: "=1234", // add an = sign
        success: function (data) {
        console.log(data);
        console.log("success");
        },
        error: function () {        
            console.log('error');
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      相关资源
      最近更新 更多