简体   繁体   English

WebApi 2-Json请求待处理

[英]WebApi 2 - Json request pending

when I call one webapi from ajax, if I return something different from simple string or int, the request is still pending. 当我从ajax调用一个webapi时,如果我返回的内容与简单字符串或int不同,则该请求仍待处理。

here my javascript: 这是我的JavaScript:

var endPoint = "/api/services/attivita/set";
$.ajax({
    url: endPoint,
    data: JSON.stringify(
        {
        'id': attivita.IDTipoAttivita,
        'descrizione': $('#Descrizione').val()
        }
    ),
    dataType: 'json',
    contentType: "application/json;charset=utf-8",
    processData: false,
    type: 'post',
    success: function (data) {
        console.log('ok');
    },
    error: function (data) {
        console.log('ko');
    }
});

and here webapi code 这里是webapi代码

    [System.Web.Http.HttpGet]
    [System.Web.Http.HttpPost]
    [System.Web.Http.Route("api/services/attivita/set")]
    public TipoAttivita SetAttivita([FromBody] dynamic obj)
    {
        var id = (int)obj.id;
        var descrizione = obj.descrizione.ToString();
        var nuovo = id == -1;

        var attivita = new TipoAttivita()
        //do stuff of attivita object
        this.CurrentDb.TipoAttivita.Add(attivita);
        this.CurrentDb.SaveChanges();

        return (attivita);
    }

If I change to "public int...." and "return(1);" 如果我改为“ public int ....”和“ return(1);” at the end of the function everything works fine. 在该功能的末尾,一切正常。

in WebApiConfig.cs I have this 在WebApiConfig.cs中,我有这个

var jsonFormatter = new JsonMediaTypeFormatter
        {
            SerializerSettings = {ReferenceLoopHandling = ReferenceLoopHandling.Ignore}
        };
        jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));

        config.Formatters.Clear();
        config.Formatters.Add(jsonFormatter);

Any idea? 任何想法? Thanks a lot 非常感谢

尝试将返回类型更改为IHttpActionResult并返回Ok(attivita)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 webapi在控制器中使用datetime反序列化JSON请求 - webapi deserialize JSON request with datetime in controller 请求使用JSON对象发送到WebApi DateTime Post? - Request to WebApi DateTime Post using JSON object? POST方法JSON WebAPI请求失败,出现400错误请求 - Post method JSON WebAPI request fails with 400 bad request 将json对象添加到Java HttpGet请求到C#WebApi - add json object to Java HttpGet request to C# WebApi 400错误的请求,通过HttpClient.PutAsync将Json提交到WebApi - 400 Bad Request submitting Json to WebApi via HttpClient.PutAsync 如何使用 http 请求从 WebAPI 返回 JSON? - How to return a JSON from WebAPI using http request? ASP.NET WebApi根据请求标头返回XML或json - ASP.NET WebApi returns XML or json based on the request header 是否可以通过带有 IOrganizationService 的 WebApi 使用 JSON 请求调用 Dynamics CRM 操作? - Is it possible to call an Dynamics CRM action with a JSON request through a WebApi with IOrganizationService? WebApi NET Core 未收到请求 json,而在 NET 标准中却收到了 - WebApi NET Core not receiving request json, whereas it does in NET standard 如何在异常处理程序 - WebAPI 中获取请求 JSON 消息? - How to get Request JSON Message with in the Exception Handler - WebAPI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM