0

我正在使用 JQuery 3 将数据发布到 API,如下所示:

$.post({ url: "api/questions", data: { content: "Content" }, dataType: "json" })
  .done(function (data, status, xhr) {
    console.log(message);
  })
  .fail(function (xhr, status, error) {
    console.log(error);
  })

当我运行它时,我收到以下错误:

Unsupported Media Type

我不确定为什么会这样。我使用 PostMan 测试了我的 API,以发送带有以下正文的 Post 请求:

{
  content: "Content"
}

它工作得很好......我错过了什么?

4

1 回答 1

2

尝试使用这个:

$.postJSON = function(url, data, callback) {
    return jQuery.ajax({
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    'type': 'POST',
    'url': url,
    'data': JSON.stringify(data),
    'dataType': 'json',
    'success': callback
    });
};

(取自这个答案)

于 2016-10-16T20:13:01.217 回答