2016-06-30 21 views
1

我想發佈一個靜態的新條目使用angularjs $ http調用,但它會拋出400個不好的請求我不明白爲什麼?在這裏,我在輸入中傳入完全相同類型的json對象。試圖發佈json在angularjs將400請求不好

下面是我要添加新記錄的主要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

從頭部取下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格式。一些東西必須丟失。