Angular JS http.post

时间:2017-06-21 12:03:24

标签: angularjs

我会使用像这样的http.post向服务器发送一些数据;

test.controller("MyController2", function ($scope, $http) {    

        var array = [];

        $scope.postdata = function (name, age, adress) {
            var dataSend = { name: name, age: age, adress: adress };                

            try {
                $http({
                    method: "POST",
                    url: "wfrmPostData.aspx/SendJsonData",
                    dataType: 'json',
                    data: JSON.stringify(dataSend),
                    headers: { "Content-Type": "application/json" }
                }).then
                (
                    function success(response) {                                                      
                        array.push(response.data.d);
                        $scope.showData2 = array;                           
                    },
                    function error(data, status, headers, config) {
                        $scope.showData = status;
                    }
                );
            }
            catch (e) {
                alert(e);
            }
        };
    });

服务器端代码在这里:

[WebMethod]
    public static string SendJsonData(string name, string age, string adress)
    {
        var keyValues = new Dictionary<string, string>
           {
               { "name", name },
               { "age", age },
               { "adress", adress }
           };

        JavaScriptSerializer js = new JavaScriptSerializer();
        string json = js.Serialize(keyValues);
        return json;
    }

myShowData2变量在ng-repeat标记中不起作用。

response.data.d得到这样的结果:{"name":"asd","age":"2","adress":"asd"}

Html代码:

<tr ng-repeat="record2 in showData2">
                <td> {{ record2.name }} </td>
                <td> {{ record2.age }} </td>
                <td> {{ record2.adress }} </td>

            </tr>

我该怎么办呢。谢谢

2 个答案:

答案 0 :(得分:0)

问题的至少一部分是这一行:

$scope.showData2 = array.push(response.data.d);  

array.push返回数组的长度。查看this link。你可以这样做:

array.push(response.data.d);  

$scope.showData2 = array;

答案 1 :(得分:0)

你没有将showData2分配给数组变量,你只是将它推入数组变量,而不是你可以使用如下

 $scope.showData2=[];
$scope.showData2.push(response.data.d);

成功回调,希望这有效