2016-10-05 195 views
-1

我是Angular js中的新成員,我不知道我的POST是否正常工作。它返回一個[object Object]!這是什麼樣的錯誤?我的意思是如果POST正在工作,那麼表單有什麼問題?爲什麼我的POST返回[object Object]

//Activity controller 
.controller('ActivityCtrl', function($scope, $rootScope, $state, $ionicLoading, $ionicScrollDelegate, PostService, $http, AuthService) { 
    var user = AuthService.getUser(); 
    $http.get("http://hannation.me/api/buddypressread/activity_get_activities_grouped/?userid=" + user.data.id) 
    .success(function(data) { 
     $scope.activities = data.activities; 
    }); 



    $scope.addActivity = function(){  
    //  
    var dataObj = { 
     new_activity : $scope.new_activity 
    }; 
    $http.post('http://hannation.me/api/userplus/activities_post_update/?key=57f211a0354d7&cookie=' 
     + user.cookie + '&content=' + dataObj).success(function(data, status, headers, config) { 
     $scope.message = data; 
    }); 

    $scope.new_activity=''; 
    }; 
}) 
<form class="row"> 
     <div class="col col-80 content col-center"> 
     <input class="new-comment-message" type="text" placeholder="Leave a comment..." ng-model="new_activity" 
     name="new_activity"></input> 
     </div> 
     <div class="col col-20 button-container col-center"> 
     <button class="button button-clear send" type="submit" ng-click="addActivity()"> 
      Send 
     </button> 
     </div> 
    </form> 
+1

你在URL中重新鏈接dataObj。您不應該將任何任意數據連接到URL中。至少,你需要在像'user.data.id'這樣的字符串周圍使用'encodeURIComponent()',但是你絕對不能直接使用'dataObj'。 – Brad

+0

謝謝!我會嘗試 –

+0

@Brad你能告訴我一些POST的工作示例嗎? –

回答

1

首先,主要是因爲這真的讓我感到困惑......使用params屬性查詢參數,並且不要使用deprecatedsuccess方法。使用params可確保您的查詢參數已過濾,以便在URL中使用(請參閱encodeURIComponent())。

$http.get('http://hannation.me/api/buddypressread/activity_get_activities_grouped/', { 
    params: { userid: user.data.id } 
}).then(function(response) { 
    $scope.activities = response.data.activities; 
}); 

其次,documentation(我假設是正確的)表示,你應該使用一個GET請求,並沒有POSTcontent似乎是一個字符串,那麼你的第二個請求應該看起來像

$http.get('http://hannation.me/api/userplus/activities_post_update/', { 
    params: { 
     key: '57f211a0354d7', 
     cookie: user.cookie, 
     content: $scope.new_activity 
    } 
}).then(function(response) { 
    // not sure about this, the documentation doesn't indicate there's a response 
    console.log('response data', response.data); 
}); 
+0

它是一種工作 這個錯誤我這個內容加斜面請求 ionic.bundle.js:23826 GET http://hannation.me/api/userplus/activities_post_update/?cookie= adminara%7C ... 4c1bdc98bd601cbb37b37f1a5cdd2f658d5de39b329fda087e3798e8&key = 57f211a0354d7 404(Not Found) –

+0

它不起作用。 –

+0

謝謝你的工作! –

-1

答案謊言你的問題中。您在簽約對象到你的消息,你應該做這樣的

$http.post('http://hannation.me/api/userplus/activities_post_update/?key=57f211a0354d7&cookie=' 
    + user.cookie + '&content=' + dataObj).success(function(data, status, headers, config) { 
    $scope.message = data.message; 
}); 

在這裏,我分配了我從POST請求所獲得的價值之一。要知道響應對象中的值是什麼,請控制響應對象併爲您的$ scope.message指定相應的值。

+0

你如何知道響應中有一個'message'屬性? – Phil

+0

我剛纔給出的例子@Phil – Chetan

相關問題