JSON.stringify数据/子数据,以便发送到webapi

时间:2017-06-16 19:50:24

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

使用以下代码,我可以将此内容传递给我的webapi 接收它像

public void Post(Business.Entities.api.newsalert alert)
{
    //do stuff here
}

category = $('#dt_category').val();
title = $('#inputTitle').val();
url = $('#inputURL').val();
comments = $('#inputComments').val();
subject = "News Alert / Alerte Nouvelles: " + title;

var dataJSON = {
    userid: username,
    to: "me@here.com",
    url: url,
    subject: subject,
    title: title,
    source: "My company",
    comments: comments,
    category: category
};

$.ajax({
    type: 'POST',
    url: "http://mywebserver/api/NewsAlerts",
    data: JSON.stringify(dataJSON),
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        q.resolve();
    }
});

但是,我的老板要我让类别选择使用多个..

因此我做了这个

    category = $('#dt_category').val(); //apples, oranges, peaches
    title = $('#inputTitle').val();
    url = $('#inputURL').val();
    comments = $('#inputComments').val();
    subject = "News Alert / Alerte Nouvelles: " + title;

    var categories = [];
    $(category).each(function (index) {
        categories.push({ 'category': category[index] });
    });

    var dataJSON = {
        userid: username,
        to: "me@here.com",
        url: url,
        subject: subject,
        title: title,
        source: "My company",
        comments: comments,
        category: categories 
    };

    $.ajax({
        type: 'POST',
        url: "http://mywebserver/api/NewsAlerts",
        data: JSON.stringify(dataJSON),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            q.resolve();
        }
    });

但是当我的webapi收到ajax Put时,它总是为空。我假设我需要以某种方式将类别变成某种子节点,但我不确定。

1 个答案:

答案 0 :(得分:1)

Your api request object should contain a property that is some collection type of a class with the property "category".

if exists (select * from inserted)
insert dbo.errEDVisitOriginal (EDVisitId, SubmissionControlID)
select EDVisitId, SubmissionControlID from inserted;
相关问题