2017-05-24 5 views
0

角度jについて学び始めています。私はポスト関数であるjavascript関数を持っています。今は角度jsに変換したいのですが。javascriptのポスト関数を角度jsに変換

<script> 
function submitFunction() { 
    var ip_range = ""; 

    var user_name = "wind"; 

    $.post("software.aspx", {"action": "getchartdata","username": user_name}, 
     function(data, status) { 

      if (status === "success") { 
       if (data) { 
        console.log(JSON.stringify(data)); 

        dataParse(data); 
       } else { 
        alert("ALERT!"); 
       } 

       alert(start_date) 
      } 
      alert(start_date) 


     }); 
} 
</script> 

私はこの古いジャバスクリプトからの戻り結果を得ることができ、それは私にJSONの結果を返します:

は、ここに私の古いjavascriptのコードです。以下は動作できなかった私の新しい角度js関数です。バックエンドコードを変更する必要があるかどうかはわかりません。

<script> 
    var app = angular.module('myApp', []); 
    app.controller('FormCtrl', function ($scope, $http) { 

     console.log("Preparing..") 
     $scope.submitForm = function() { 
      console.log("posting data...."); 
      var username = $scope.form; 
      $http({ 
       method: 'POST', 
       url: 'Software.aspx', 
       data: {username: username}, 


      }).then(function successCallback(response) { 
       // this callback will be called asynchronously 
       // when the response is available 
       console.log("Success") 
       console.log(response) 

      }, function errorCallback(response) { 
       // called asynchronously if an error occurs 
       // or server returns response with an error status. 
       console.log("fail") 

      }); 

     }; 
    }); 
</script> 

私は角度のあるjsに変換することを私に教えてくれませんか?ありがとう。

+1

あなたは 'url: 'Software.aspx'、' – Satpal

+0

のアクションを指定していません。コンソールログを確認してください –

+0

どうしたらうまくいかないのですか?結果にHTTPステータスがありますか? – Mistalis

答えて

1

私はこのメソッドがJavaScriptから同じ結果を得ることがわかった。

$scope.submitForm = function() { 
      console.log($scope.selected) 

      console.log(startDate) 
      $http({ 
       method: 'POST', 
       url: 'Software.aspx', 
       data: { 
        "action": "getchartdata", 
        "username": $scope.username, 

       }, 
       headers: { 
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 
       }, 
       transformRequest: function (data) { 
        return $.param(data); 
       } 
      }).then(function successCallback(response) { 
       // this callback will be called asynchronously 
       // when the response is available 
       console.log(response); 
       dataParse(response.data); 


      }, function errorCallback(response) { 
       // called asynchronously if an error occurs 
       // or server returns response with an error status. 
       console.log("fail to send") 

      }); 

     }; 

これにヘッダを設定する必要があります。そして、これだけの意志が働く

headers: { 
         'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 
        }, 
        transformRequest: function (data) { 
         return $.param(data); 
        } 

関連する問題