【问题标题】:Unsupported Media Type when Posting JSON data to API using JQuery使用 JQuery 将 JSON 数据发布到 API 时不受支持的媒体类型
【发布时间】:2016-10-17 04:13:01
【问题描述】:

我正在使用 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"
}

而且效果很好......我错过了什么?

【问题讨论】:

标签: jquery jquery-3


【解决方案1】:

试试这个:

$.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
    });
};

(取自this答案)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-25
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多