试图在angularjs中发布json会有400个错误的请求

时间:2016-06-30 19:03:55

标签: angularjs

我正在尝试使用angularjs $ http调用发布静态新条目,但它会抛出400个错误请求我不明白为什么?在这里,我在输入中传递完全相同类型的json对象。

下面是我想要添加新记录的主要json文件。

{
    "count": 384,
    "next": "http://104.197.128.152:8000/v1/tracks?page=2",
    "previous": null,
    "results": [
        {
            "id": 38,
            "title": "Hey Jude",
            "rating": "4.9",
            "genres": [
                {
                    "id": 5,
                    "name": "ramesh"
                }
            ]
        },
        {
            "id": 39,
            "title": "hello adele",
            "rating": "4.0",
            "genres": [
                {
                    "id": 4,
                    "name": "bollywood"
                },
                {
                    "id": 8,
                    "name": "metakai"
                }
            ]
        }
    ]
}

我想发布的数据:

var data = {
    "id": 79,
    "title": "new song anand",
    "rating": "4.0",
    "genres": [
        {
            "id": 4,
            "name": "bollywood"
        },
        {
            "id": 8,
            "name": "metakai"
        }
    ]
}

发布数据的语法:

$http({
          method: 'POST',
          url: 'http://104.197.128.152:8000/v1/tracks',
          dataType: 'json',
          data: data,
          headers: { 'Content-Type': 'application/json; charset=UTF-8' }
          }).success(function (data) {
            console.log(data,"success");
          }).error(function (data) {
            console.log(data,"fail");
          });

1 个答案:

答案 0 :(得分:1)

从标题中删除dataType: 'json'charset=UTF-8

$http({ method: 'POST', url: 'http://104.197.128.152:8000/v1/tracks', data: data, headers: { 'Content-Type': 'application/json'} }).success(function (data) { console.log(data,"success"); }).error(function (data) { console.log(data,"fail"); });

必须有效。

如果不重新检查您的JSON格式。必须缺少一些东西。