2016-10-05 14 views
-1

私はAngular jsで新しく、POSTが機能しているかどうかわかりません。 [オブジェクトオブジェクト]を返します!どのようなエラー? POSTが動作している場合は、フォームに何か問題がありますか?あなたの質問内の私のPOSTが[オブジェクトオブジェクト]を返す理由

//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

のようになりますように文字列であることを、ないPOSTcontent表示されますがGET要求を使用する必要があることを示しますURL内の 'dataObj'を連結します。任意のデータをURLに連結しないでください。少なくとも 'user.data.id'のような文字列の周りでは' encodeURIComponent() 'を使う必要がありますが、そのまま' dataObj'を使うことはできません。 – Brad

+0

ありがとう!私は試してみます –

+0

@ブラッド私は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はあなたの2番目の要求が

$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

@Phil私はちょうど私がこのコンテンツカント ionic.bundle.jsを要求するために追加の例を与えている:23826 GET http://hannation.me/api/userplus/activities_post_update/?cookie= adminara%7C ... 4c1bdc98bd601cbb37b37f1a5cdd2f658d5de39b329fda087e3798e8&key = 57f211a0354d7 404(見つかりません) –

+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; 
}); 

ここで私は投稿要求から得ている値の1つを割り当てました。応答オブジェクト内の値が何であるかを知るには、応答オブジェクトをコンソール化し、$ scope.messageに適切な値を割り当てます。

+0

レスポンスに 'message'プロパティがあることをどうやって知っていますか? – Phil

+0

それは一種 このエラーを働いていた – Chetan

関連する問題