Ajax进行post请求时-- 415 Unsupported media type

本来想用Ajax进行一个小小的请求测试:

    function test() {
    $.ajax({
        type: "POST",
        async: false,
        url: "https://10.8.62.231:28001/api/inv-basic/v1/mes/ex_querier?csrftoken=e80b771c0de9fafb9910fb9161233122",
        data: [{ "key": "displayName", "value": ["N3"] }, { "key": "hasLabel", "value": ["1"] }],
        success: function (data) {
            console.log(data);
        }
    })

无奈直接报错:

415 (Unsupported Media Type)

查询资料发现没有设置contentType,

1.Ajax请求时没有设置Content-Type为Json。
2.发送的请求内容不要转成JSON对象,直接发送JSON字符串即可,否则就415错误!

改代码:

    function test() {

    $.ajax({
        type: "POST",
        async: false,
        url: "https://10.8.62.231:28001/api/umebn-inv-basic/v1/mes/ex_querier?csrftoken=e80b771c0de9fafb9910fb9161233122",
        contentType: 'application/json',
        data: '[{"key": "displayName", "value": ["NE32003"]}, {"key": "hasLabel", "value": ["1"]}]',
        success: function (data) {
            console.log(data);
        }

    })

搞定!

你可能感兴趣的:(javascript)