2012-10-08 24 views
6

我想寫一個AngularJS資源模塊,將一些數據發佈到服務器。默認的內容類型看起來是「application/xml」。我試圖覆蓋內容類型爲「application/x-www-form-urlencoded」。當做一個正常的$ http.post()我可以設置內容類型,當我檢查Firebug時,我可以看到它設置正確。當我使用資源的POST方法時,我無法從默認值更改內容類型。我認爲我按照documentation的描述來做。AngularJS資源沒有設置內容類型

http://jsfiddle.net/vBsUH/3/

var myApp = angular.module('myApp',['myResource']); 

angular.module('myResource', ['ngResource']).factory('myResource', function($resource){ 
    return $resource('/echo/json',{},{ 
     add:{ method:'POST', params:{ foo:'1' }, headers:{'Content-Type':'application/x-www-form-urlencoded'} } 
    }); 
}); 

function MyCtrl($scope,$http,myResource) { 
    $scope.click = function() { 

     //This will make an ajax call with a content-type of application/xml  
     myResource.add(); 

     //this will make an ajax call with a content-type of application/x-www-form-urlencoded 
     $http.post('/echo/json','foo=1',{'headers':{'Content-Type':'application/x-www-form-urlencoded'}}); 

    } 
}​ 

如何獲得一個AngularJS資源發佈與不同的內容類型的任何意見或例子將不勝感激。

回答

1

傑克,昨天我提供了一些信息,以一個類似的問題:https://stackoverflow.com/a/12876784/1367284

粘貼該響應如下:

雖然開發文檔(如10月12日)的顯示,覆蓋頭部可能在$資源,它尚未發佈(v1.0.2或v1.1.0)。但是,該功能位於v1.0.x和主分支中。爲了獲得該功能,您現在可以考慮從v1.0.x分支開始構建。

如何打造:http://docs.angularjs.org/#H1_4

或者,你可以從快照生成拉:http://code.angularjs.org/snapshot/

貌似這個功能將在下一版本中。