【问题标题】:Failed to load resource: the server responded with a status of 500 (Internal Server Error) while httpPost加载资源失败:服务器响应状态为 500(内部服务器错误),而 httpPost
【发布时间】:2017-07-14 17:27:01
【问题描述】:

我尝试通过 Angular 调用 C# 函数 我看到了一个模板怎么做,我得到了这个错误。 o 试图在 google 中寻找解决方案,但这是一般问题。 我很确定我没有正确调整我的代码 这是javascript代码:

 var app = angular.module("loginApp", []);
    app.controller("loginC", ['$scope', '$http',function ($scope, $http) {
        $scope.login = function () {
           // $http.post('/login.aspx/Login', { userName: $scope.vm.username, password: $scope.vm.password });
            var httpreq = {
                method: 'POST',
                url: 'login.aspx/Login',                    
                data: { userName: $scope.vm.username, password: $scope.vm.password }
            }
            $http(httpreq).success(function (response) {

                alert("Saved successfully.");
            })
        };



    }]);

这是 C# 函数声明(该函数驻留在文件后面的登录代码中):

 protected void Login(String userName,String password)
{
 //some code
}

更新

我通过将“headers”添加到 httpreq 变量来更改 HTTP 的配置

var httpreq = {
       method: 'POST',
       url: '/login.aspx/Login',
       headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'},                     
                data: { username: $scope.vm.username, password: $scope.vm.password }
            }
            $http(httpreq).success(function () {

                alert("Saved successfully.");
            })

错误消失并弹出警报,但我将BP放在服务器端函数的开头,我仍然无法到达那里,

我已经尝试添加 [webmethod] 和 [HttpPost] 装饰。

更新2

当我将网址更改为

url: 'login.aspx'

(没有函数名称)并且我在 PageLoad 函数中设置了一个 BP,我成功地到达了服务器端的 BP,这意味着问题出在函数的路径上。 我该怎么办?

【问题讨论】:

  • 看到这是一个 500(服务器端)错误,我们需要查看服务器端 (C#) 代码
  • ן 写了函数的声明,这是整个代码
  • 如果这真的是“整个代码”,那么问题应该很明显——它根本没有做任何事情
  • 我真的问这有什么关系?我在函数的开头设置了一个 BP,但它之前失败了
  • 目前我只有“ response.Redirect("Default.aspx"); ”来检查我是否到达了这个BP

标签: javascript angularjs http http-post


【解决方案1】:

试试这个

var httpreq = {
       method: 'POST',
       url: '/login.aspx/Login',
       headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'},                     
                data: { userName: $scope.vm.username, password: $scope.vm.password }
            }
            $http(httpreq).success(function () {

                alert("Saved successfully.");
            })


 [WebMethod]
 [HttpPost]
 protected static void Login(String userName,String password)
{
 //some code
}

你在传递username,而你在camelCase的webmethod中使用,即userName。也可以使用static web 方法。

【讨论】:

  • 我没有收到任何错误,它没有在服务器端到达我的 BP
  • 你看到我的更新了吗?现在的 C# 代码只有 int x; x=6;我在 int x 上设置了一个 BP;
猜你喜欢
  • 2013-04-16
  • 1970-01-01
  • 2018-01-22
  • 2014-11-24
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多