Javascript AngularJS Http Post-500内部服务器错误

Javascript AngularJS Http Post-500内部服务器错误,javascript,angularjs,asp.net-mvc,Javascript,Angularjs,Asp.net Mvc,我知道还有很多其他的500(内部服务器错误)问题,但阅读这些问题对我的情况仍然没有帮助 我正在研究两个angularjs方法,其中包含$HTTPPOST调用 第一个问题 第一个命令成功命中服务器端控制器,并按预期返回一个JSON对象,但我的下拉列表没有被工厂调用的结果填充 我关心的一件事是控制台输出。(正如您将在下面的代码中看到的,我有几个打印语句。)打印URL,然后将我的下拉选项字段打印为未定义,然后服务器的响应与响应一起返回。我需要告诉JS等待响应吗?某种异步 第二个问题 我有完全相同的h

我知道还有很多其他的
500(内部服务器错误)
问题,但阅读这些问题对我的情况仍然没有帮助

我正在研究两个angularjs方法,其中包含
$HTTPPOST
调用

第一个问题

第一个命令成功命中服务器端控制器,并按预期返回一个JSON对象,但我的下拉列表没有被工厂调用的结果填充

我关心的一件事是控制台输出。(正如您将在下面的代码中看到的,我有几个打印语句。)打印URL,然后将我的下拉选项字段打印为
未定义
,然后服务器的响应与响应一起返回。我需要告诉JS等待响应吗?某种异步

第二个问题

我有完全相同的
httppost
方法调用(但有新的变量),我得到
500(内部服务器错误)
错误消息。此调用未命中服务器端ctrl方法(基于ctrl中的断点)

问题

  • 我如何成功地用测试结果填充下拉列表
    $http.post
    呼叫
  • 为什么我的第二个
    $http.post
    方法创建
    500(内部服务器错误)
  • Javascript

    'use strict';
    var app = angular.module('LineModule', ['ngMaterial']);
    
    app.controller("LineCtrl", ['$scope', '$http', 'LineService',
    function ($scope, $http, LineService) {
        $scope.Types = LineService.get_DropDownList("Type");
        console.log("Types:"); console.log($scope.Types);
        $scope.Statuses = LineService.get_DropDownList("Status");
        $scope.FundingSources = LineService.get_DropDownList("Funding_Source");
        ...
    
        $scope.get_DeptLeader = function () {
            $scope.SelectedLeader = LineService.get_DeptLeader($scope.CPOC_Title, $scope.CostCenter_ID);
        };
    
    }]);
    
    app.factory('LineService', ["$http", function ($http) {
        return {
            ...
    
    
            ***FIRST METHOD***
            get_DropDownList: function (field) {
                var info = {
                    section: 'Line',
                    field: field
                };
    
                var URL = getBaseURL() + 'DBO/DropDown_GetList';
                console.log(URL);
                var DropDown;
                $http({
                    method: 'POST',
                    url: URL,
                    data: JSON.stringify(info),
                })
                .then(function (response) {
                    if (response !== 'undefined' && typeof (response) == 'object') {
                        DropDown = response.data;
                        console.log(DropDown);
                        return DropDown;
                    }
                    else {
                        console.log('ERROR:');
                        console.log(response);
                        return 'No Options Found';
                    }
                });
            },
    
    
            ***SECOND METHOD***
            get_DeptLeader: function (CPOC_Title, CostCenter_ID) {
                console.log("call get leader");
    
                if (CPOC_Title < 1 || CPOC_Title == undefined) { return "No Title Selected"; }
                if (CostCenter_ID < 1 || CostCenter_ID == undefined) { return "No Cost Center Selected"; }
    
                console.log(CPOC_Title);
                console.log(CostCenter_ID);
    
                var info = {
                    Leader_ID: CPOC_Title,
                    CostCenter_ID: CostCenter_ID
                };
    
                var URL = getBaseURL() + 'DBO/DeptLeader_GetCurrent';
                console.log(URL);
                var DeptLeaders;
                $http({
                    method: 'POST',
                    url: URL,
                    data: JSON.stringify(info),
                })
                .then(function (response) {
                    if (response !== 'undefined' && typeof (response) == 'object') {
                        DeptLeaders = response.data;
                        console.log(DeptLeaders);
                        return DeptLeaders;
                    }
                    else {
                        console.log('ERROR:');
                        console.log(response);
                        return 'No Options Found';
                    }
                });
            },
        };
    }]);
    
    “严格使用”;
    var app=angular.module('LineModule',['ngMaterial']);
    app.controller(“LineCtrl”[“$scope”,“$http”,“LineService”,
    函数($scope、$http、LineService){
    $scope.Types=LineService.get_DropDownList(“Type”);
    console.log(“类型”);console.log($scope.Types);
    $scope.Statuses=LineService.get_DropDownList(“状态”);
    $scope.FundingSources=LineService.get_DropDownList(“资金来源”);
    ...
    $scope.get\u DeptLeader=函数(){
    $scope.SelectedLeader=LineService.get\u DeptLeader($scope.CPOC\u Title,$scope.CostCenter\u ID);
    };
    }]);
    app.factory('LineService',[“$http],函数($http){
    返回{
    ...
    ***第一种方法***
    get_DropDownList:函数(字段){
    变量信息={
    “行”部分,
    字段:字段
    };
    var URL=getBaseURL()+'DBO/DropDown_GetList';
    console.log(URL);
    var下拉列表;
    $http({
    方法:“POST”,
    url:url,
    数据:JSON.stringify(info),
    })
    .然后(功能(响应){
    if(响应!=“未定义”&&typeof(响应)==“对象”){
    DropDown=response.data;
    console.log(下拉菜单);
    返回下拉列表;
    }
    否则{
    console.log('ERROR:');
    控制台日志(响应);
    返回“未找到选项”;
    }
    });
    },
    ***第二种方法***
    get_DeptLeader:函数(CPOC_标题、成本中心ID){
    日志(“调用获取领导者”);
    如果(CPOC_Title<1 | | CPOC_Title==未定义){返回“未选择标题”}
    如果(CostCenter_ID<1 | | CostCenter_ID==未定义){返回“未选择成本中心”;}
    控制台日志(CPOC_标题);
    控制台日志(成本中心ID);
    变量信息={
    领导ID:CPOC\U职务,
    成本中心标识:成本中心标识
    };
    var URL=getBaseURL()+'DBO/DeptLeader_GetCurrent';
    console.log(URL);
    var分路器;
    $http({
    方法:“POST”,
    url:url,
    数据:JSON.stringify(info),
    })
    .然后(功能(响应){
    if(响应!=“未定义”&&typeof(响应)==“对象”){
    DeptLeaders=response.data;
    控制台日志(DeptLeaders);
    返回卸料器;
    }
    否则{
    console.log('ERROR:');
    控制台日志(响应);
    返回“未找到选项”;
    }
    });
    },
    };
    }]);
    
    DBO控制器:服务器端

    [HttpPost]
    public JsonResult DeptLeader_GetCurrent(string Leader_ID, string CostCenter_ID)
    {
        try {
            List<SelectListItem> DL = DB.DeptLeader_GetByIDs(Leader_ID, CostCenter_ID);
            return Json(DL, JsonRequestBehavior.AllowGet);
        }
        catch (Exception e){
             string error = e.ToString();
             return null;
         }
    }
    
    [HttpPost]
    public JsonResult DropDown_GetList(Sections section, DropDownFields field)
    {
        return Json(DB.GetDropDownValues(section, field), JsonRequestBehavior.AllowGet);
    }
    
    [HttpPost]
    public JsonResult DeptLeader\u GetCurrent(字符串头\u ID,字符串成本中心\u ID)
    {
    试一试{
    List DL=DB.DeptLeader\u getbyid(Leader\u ID,CostCenter\u ID);
    返回Json(DL,JsonRequestBehavior.AllowGet);
    }
    捕获(例外e){
    字符串错误=e.ToString();
    返回null;
    }
    }
    [HttpPost]
    public JsonResult下拉列表(Sections部分,DropDownFields字段)
    {
    返回Json(DB.GetDropDownValues(节,字段),JsonRequestBehavior.AllowGet);
    }
    
    CSHTML

    <div ng-app="LineModule" ng-controller="LineCtrl" class="container col-md-12">
        ...
        <select ng-model="Type_ID" class="form-control" required>
            <option ng-value="T.Value" ng-repeat="T in Types">{{T.Text}}</option>
        </select>
        ...
        <select ng-model="CPOC_Title" class="form-control" ng-change="get_DeptLeader()">
            <option ng-value="D.Value" ng-repeat="D in DeptLeaders">{{D.Text}}</option>
        </select>
        {{SelectedLeader}}
        ...
    </div>
    
    
    ...
    {{T.Text}}
    ...
    {{D.Text}}
    {{SelectedLeader}}
    ...
    
    您是否尝试在
    try catch
    上设置断点以查看是否引发异常?可能是因为
    领导ID
    成本中心ID
    都作为
    int
    发送,服务器希望这两个500都有
    字符串。如果服务器出错,您将无法执行任何操作。问一下你的后端人员。问题一,你的问题可能与异步有关。等待加载页面,直到返回结果,这会有所帮助。类似于$scope.isLoaded=false的东西,当文章被完整设置时$scope.isLoaded=true.Taki-我在
    catch
    上有一个断点,它也没有被命中。Taki-我尝试将代码更改为int,但try或catch中仍然没有按ctrl键。我是后端开发人员/完整堆栈开发人员。