2017-04-05 136 views
0

我正在嘗試發送帶有標題和請求正文的請求。到目前爲止,我已經達到了這一點:ngResource:Angularjs - 發送帶有標題和請求正文的請求

createJob: function(jobName, jobAddress, jobContact, jobComments) { 
        var payload = { 
         name: jobName, 
         address: jobAddress, 
         contact: jobContact, 
         comments: jobComments 
        }; 
        console.log(payload); 
        return $resource(route, {}, { 
         save: { 
          method: 'POST', 
          header: {'Content-Type': 'application/json'}, 
          transformRequest: function(data){ 
           console.log('Data in transform request is'); 
           console.log(data); 
           return data; // this will go in the body request 
          } 
         } 
        }); 
       } 

我不知道在哪裏放置有效載荷在這種情況下,任何幫助嗎?還打電話時,我目前正在嘗試做類似的事情:

createJob(this.jobName, this.jobAddress, this.jobContact, this.jobComments). 
        save().$promise. 
        then(function (response) { 
         console.log('Create job response is'); 
         console.log(response); 
        }). 
        catch(function (error) { 
         console.log('Create job error is'); 
         console.log(error); 
        }); 

任何幫助,將不勝感激!

回答

0

我來到了一個解決方案,任何人有興趣:

createJob: function(jobName, jobAddress, jobContact, jobComments) { 
      var payload = { 
       name: jobName, 
       address: jobAddress, 
       contact: jobContact, 
       comments: jobComments 
      }; 
      console.log(payload); 
      return $resource(route, {}, { 
       save: { 
        method: 'POST', 
        transformRequest: function(data){ 
         console.log('Data in transform request is'); 
         console.log(data); 
         return angular.toJson(data); // this will go in the body request 
        } 
       } 
      }).save({}, payload); 
     } 

createJob(this.jobName, this.jobAddress, this.jobContact, this.jobComments).$promise. 
      then(function (response) { 
       console.log('Create job response is'); 
       console.log(response); 
       //Refresh the page so newly created job can be seen 
       window.location.reload(); 
      }). 
      catch(function (error) { 
       console.log('Create job error is'); 
       console.log(error); 
      });