WebApi 2 - Json请求等待

时间:2016-09-13 15:23:40

标签: c# json asp.net-web-api

当我从ajax调用一个webapi时,如果我返回的内容与简单字符串或int不同,则请求仍处于待处理状态。

这里是我的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');
    }
});

这里是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);
    }

如果我改为" public int ...."和"返回(1);"在功能结束时一切正常。

WebApiConfig.cs中的

我有这个

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

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

有什么想法吗? 非常感谢

1 个答案:

答案 0 :(得分:0)

尝试将返回类型更改为select month(savings_date), sum(amount) from net_savings where dateadd(savings_date, interval -2 month) group by month(savings_date); 并返回IHttpActionResult