简体   繁体   English

Angular JS Json数据

[英]Angular JS Json data

I am self educating myself in angular js. 我正在用js自我教育自己。 For this i have created a project modelled after an actual project in my job 为此,我创建了一个以工作中的实际项目为模型的项目

All get operation work fine but POST is giving me issue 一切正常,但POST给我问题

My controller.js 我的controller.js

var ngapp = angular.module('ngWebApp', ['angularUtils.directives.dirPagination']);
ngapp.controller('ngIndexController', function ($scope, ngDashboardService) {
 $scope.exportData = function (selectedDataList) {

    var getData = ngDashboardService.AddReportAudit(selectedDataList)
    getData.then(function (result) {
            alert(result.data);
        }, function () {
            alert('Error in getting records');
        });        
    };
});

My service.js 我的service.js

angular.module('ngWebApp').service("ngDashboardService", function ($http) {
    this.AddReportAudit = function (dataList) {
        var response = $http({
            method: "POST",
            url: "/Home/AddReportAudit",
            data: {
            'dataList': JSON.stringify(dataList)
            }
        });
        return response;
    };
});

My code of JasonResult in HomeController 我在HomeController中的JasonResult代码

 public JsonResult AddReportAudit(List<ADTOWebSMARTT_SSOData> dataList)
 {
    if (dataList != null)
    {
        using (HRMSystemEntities contextObj = new HRMSystemEntities())
        {
            var itemList = dataList.Where(x => x.IsChecked == true).ToList();
            itemList.ForEach(a => a.DateChecked = DateTime.Now);
            contextObj.SaveChanges();
            return Json(new { success = true });
        }
    }
    else
    {
        return Json(new { success = false });
    }
}

The problem occurs here public JsonResult AddReportAudit(List dataList) For some reason the dataList on reaching AddReportAudit become empty ie list has zero element. 这里发生问题。公共JsonResult AddReportAudit(List dataList)由于某种原因,到达AddReportAudit的dataList变为空,即list的元素为零。 dataList has 30 records in controller.js and service.js. dataList在controller.js和service.js中有30条记录。

I am not sure why that is happening. 我不确定为什么会这样。 is there a parsing that I am missing when json data comes from angular to c# 当json数据从角度到C#时,我是否缺少解析

You are actually sending an Object, so the data that reaches your public JsonResult AddReportAudit(....isAnObject), but you are expecting it to be a list. 您实际上是在发送一个Object,所以到达公共JsonResult AddReportAudit(.... isAnObject)的数据,但是您希望它是一个列表。 Just change your controller code to the snippet below, it should work. 只需将您的控制器代码更改为下面的代码段,即可正常工作。

angular.module('ngWebApp').service("ngDashboardService", 
    function($http) {
       this.AddReportAudit = function (dataList) {
          var response = $http({
             method: "POST",
             url: "/Home/AddReportAudit",
             data:JSON.stringify(dataList)
          });
       return response;
    };
});

Solution for angular 1. Place your web service url and change the json data format as per your need. 角度的解决方案1.放置您的Web服务URL并根据需要更改json数据格式。 It works. 有用。

<!DOCTYPE html>
<html>
<head>
    <title>Angular HTTP service</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
    <table>
        <tr>
            <th>S No</th>
            <th>Source Name</th>
        </tr>
        <tr ng-repeat="res in result">
            <td>{{$index + 1}}</td>
            <td>{{res.source_name}}</td>
        </tr>


    </table>
    <script type="text/javascript">
        var app = angular.module("myApp", []);
        app.controller("myCtrl", function($scope, $http){
            $http.get("http://127.0.0.1:4000/all_source").then(function(res){
                //console.log(res.data.result);
                //console.log(res);
                $scope.result = res.data.result;
            });
        });
    </script>
</body>
</html>

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM