Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 来自AngularJS的HttpPost调用引发500内部服务器错误_Javascript_Html_Angularjs_Asp.net Mvc_Asp.net Web Api - Fatal编程技术网

Javascript 来自AngularJS的HttpPost调用引发500内部服务器错误

Javascript 来自AngularJS的HttpPost调用引发500内部服务器错误,javascript,html,angularjs,asp.net-mvc,asp.net-web-api,Javascript,Html,Angularjs,Asp.net Mvc,Asp.net Web Api,我正在开发一个应用程序,需要向数据库中添加一个用户,因此需要后期操作。 但是,每次运行应用程序时,POST api调用都会出现500个内部服务器错误 下面是我的代码片段 angular.module('app') .constant('userApiRoot', 'http://localhost:82/api/User/'); Service.js app.factory('userService', [ 'userApiRoot', '$http',

我正在开发一个应用程序,需要向数据库中添加一个用户,因此需要后期操作。 但是,每次运行应用程序时,POST api调用都会出现500个内部服务器错误

下面是我的代码片段

angular.module('app')
            .constant('userApiRoot', 'http://localhost:82/api/User/');
Service.js

app.factory('userService', [
        'userApiRoot', '$http', '$q', function (apiRoot, $http, $q) {
            'use strict';
            return {

                apiRoot: apiRoot,
                addUser: function (userData) {
                    var def = $q.defer();

                    $http.post(apiRoot + "Add", JSON.stringify(userData))
                        .then(function (response) {
                            def.resolve(response.d`enter code here`ata);
                        }, def.reject);

                    return def.promise;
                }
            };
        }
]);
Controller.js

app.controller('BlogInfoController', ['userService', '$scope', BlogInfoController]);

function BlogInfoController(userService, $scope) {
    'use strict';
    $scope.user = {};

    userService.addUser($scope.user).then(function (data) {
        var userData = data;
    });
}
HTML


留言
发表评论

设置字符串化post数据,尝试将其作为原始数据发送。像这样:

$http.post(apiRoot + "Add", userData)
     .then(function (response) {
            def.resolve(response.d`enter code here`ata);
     });

在整理post数据时,尝试将其作为原始数据发送。像这样:

$http.post(apiRoot + "Add", userData)
     .then(function (response) {
            def.resolve(response.d`enter code here`ata);
     });

也许您应该尝试将数据作为对象发送

...
$http.post(apiRoot + "Add", userData)
...
为什么不直接返回http承诺呢

addUser: function (userData) {                    
                return $http.post(apiRoot + "Add", JSON.stringify(userData))
            }

也许您应该尝试将数据作为对象发送

...
$http.post(apiRoot + "Add", userData)
...
为什么不直接返回http承诺呢

addUser: function (userData) {                    
                return $http.post(apiRoot + "Add", JSON.stringify(userData))
            }
使用

并将其传递给

$http.post(apiRoot+“添加”,JSON.stringify(userData),config)

因为您将JSON数据用于post方法,并且不提供任何头。您的邮递员已包含此标题

使用

并将其传递给

$http.post(apiRoot+“添加”,JSON.stringify(userData),config)


因为您将JSON数据用于post方法,并且不提供任何头。您的邮递员已包含此标题

实际上一切都是正确的,但API返回值为500。。检查API是否正确。在C#端调试


实际上一切都是正确的,但API返回的是500。。检查API是否正确。在C#端调试


感谢所有人为我提供的优秀解决方案。最后我自己找到了解决这个问题的办法。也许有一天它会帮助别人

  • 将数据传递给angular service函数时删除了JSON.stringify
  • 虽然我已将ng submit=“addUser()”添加到表单中,但控制器中没有相应的函数可用。这是我控制器中的服务呼叫

    userService.addUser($scope.user).then(函数(数据){ var userData=数据; });

  • 将其更改为以下内容

    $scope.addUser = function () {
    userService.addUser($scope.user).then(function (data) {
                var userData = data;
            });
    }
    
    这些看起来很小的失误后,发现它,但它可以卡住你一段时间,如果不密切监测


    谢谢大家的帮助

    感谢大家为我提供了很好的解决方案。最后我自己找到了解决这个问题的办法。也许有一天它会帮助别人

  • 将数据传递给angular service函数时删除了JSON.stringify
  • 虽然我已将ng submit=“addUser()”添加到表单中,但控制器中没有相应的函数可用。这是我控制器中的服务呼叫

    userService.addUser($scope.user).then(函数(数据){ var userData=数据; });

  • 将其更改为以下内容

    $scope.addUser = function () {
    userService.addUser($scope.user).then(function (data) {
                var userData = data;
            });
    }
    
    这些看起来很小的失误后,发现它,但它可以卡住你一段时间,如果不密切监测


    谢谢大家的帮助

    如果有一个500响应,这是一个坏连接,最好放在chromedo的网络控制台上,不要将post参数转换为stringify。删除stringify后,我得到了这个错误:TypeError:(中间值)(中间值)(中间值)(中间值)不是BlogInfoController.js:38上的构造函数,请显示您的C#codepostman可以工作。但是从c#with angularjs请求运行正常。如果有500响应,连接不好,最好放在chromedo的网络控制台上。不要将post参数转换为stringify删除stringify后,我得到了以下错误:TypeError:(中间值)(中间值)(中间值)(中间值)不是BlogInfoController.js:38上的构造函数,请显示您的C#codepostman可以工作。但是从c#with angularjs请求是正确的。在您建议的更改之后。TypeError:(中间值)(中间值)(中间值)(中间值)不是BlogInfoController.js:38的构造函数。您可能有语法错误。你能在BlogInfoController.js plsw中显示我写过的文章中的行吗?在你建议的更改之后,没有任何更改。TypeError:(中间值)(中间值)(中间值)(中间值)不是BlogInfoController.js:38的构造函数。您可能有语法错误。你能展示一下BlogInfoController.js plsw中的行吗?我写的东西都在帖子中,没有任何改变。TypeError:(中间值)(中间值)(中间值)(中间值)不是BlogInfoController的构造函数。js:38TypeError:(中间值)(中间值)(中间值)(中间值)不是BlogInfoController.js:38的构造函数在原始帖子的末尾有一个注释,请检查一下。像这样试试。。。var api=apiRoot+“添加”;var sendData=JSON.stringify(userData)$http.post(api,sendData);在原始帖子的末尾有一个便条,请检查一下。像这样试试。。。var api=apiRoot+“添加”;var sendData=JSON.stringify(userData)$http.post(api,sendData);