角度发布到MVC Web API问题

编程入门 行业动态 更新时间:2024-10-25 16:20:53
本文介绍了角度发布到MVC Web API问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想学习角度。我目前正在学习 $ http 对我的MVC API的请求。 编辑:慢慢发现问题,它与将数据从表单推送到数组有关。我得到的错误信息是TypeError:将循环结构转换为JSON 我有一个简单的,我的意思是简单的HTML表单,可以让我收集2件信息并将其收集到Angularjs数组中,然后将其传递给WEB API方法。 但我的主要问题是,如果我调用参数并且输入参数的值为空,则拼写参数不正确。如果我正确拼写参数,它根本不会调用该方法。任何帮助我了解正在发生的事情的指针? 谢谢 Simon

< div ng-app = 演示 > < div ng-controller = MyController > < ; 输入 type = text id = username 名称 = 用户名 占位符 = name ... ng-model = user.name / > < 输入 type = text id = otherdetails name = otherdetails 占位符 = name ... ng-model = user.name / > < 按钮 类型 = 按钮 ng-click = 添加(用户) > 保存< / button > < button type = 按钮 ng -click = SaveUsers() > 保存到Web API < / button > < / div > < / div >

我的Angular代码如下

angular.module(' 演示',[]); angular.modeul(' 演示')。controller(' ButtonCtrl', function ($ scope,$ http){ $ scope.user = []; $ scope.Add = function (user){ $ scope.user .push(user); }; $ scope.SaveUsers = function (){ var request = $ http({ method: post , url:' / Home / Index', data:{user:$ scope.user} }); request.success( function (){ alert(' success'); }); request.error( function (){ alter(' failed'); }); }; };

我的C#代码

[HttpPost] public ActionResult Index(List< Person> user) { List< Models.Person> data = new List< Models.Person>(); return null ; } public class Person { public string 名称{获取 ; set ;} public string 此处{获取; 设置;} }

解决方案

http 对我的MVC API的请求。 编辑:慢慢发现问题,它与将数据从表单推送到数组有关。我得到的错误信息是TypeError:将循环结构转换为JSON 我有一个简单的,我的意思是简单的HTML表单,可以让我收集2件信息并将其收集到Angularjs数组中,然后将其传递给WEB API方法。 但我的主要问题是,如果我调用参数并且输入参数的值为空,则拼写参数不正确。如果我正确拼写参数,它根本不会调用该方法。任何帮助我了解正在发生的事情的指针? 谢谢 Simon

< div ng-app = 演示 > < div ng-controller = MyController > < ; 输入 type = text id = username 名称 = 用户名 占位符 = name ... ng-model = user.name / > < 输入 type = text id = otherdetails name = otherdetails 占位符 = name ... ng-model = user.name / > < 按钮 类型 = 按钮 ng-click = 添加(用户) > 保存< / button > < button type = 按钮 ng -click = SaveUsers() > 保存到Web API < / button > < / div > < / div >

我的Angular代码如下

angular.module(' 演示',[]); angular.modeul(' 演示')。controller(' ButtonCtrl', function (

scope,

http){

Im trying to learn angular. I am currently in the stage of learning about $http requests to my MVC API. EDIT: Slowly discovering the problem, it has something to do with the the pushing of the data from form into the array. the error message I am getting back is "TypeError: Converting circular structure to JSON" I have a simple and I mean simple HTML form that allows me to collect 2 pieces of information and collect it into an Angularjs array and then pass it to the WEB API method. But my main problem is that If I spell the parameter incorrectly if calls the method and the value of the input parameter is blank. If I spell the parameter correctly it doesn't call the method at all. any pointer to help me learn what is going on? Thanks Simon

<div ng-app="Demo"> <div ng-controller="MyController"> <input type="text" id="username" name="username" placeholder="name..." ng-model="user.name"/> <input type="text" id="otherdetails" name="otherdetails" placeholder="name..." ng-model="user.name"/> <button type="button" ng-click="Add(user)">Save</button> <button type="button" ng-click="SaveUsers()">Save To Web API</button> </div> </div>

My Angular code is as follows

angular.module('Demo', []); angular.modeul('Demo').controller('ButtonCtrl', function($scope, $http) { $scope.user = []; $scope.Add = function(user) { $scope.user.push(user); }; $scope.SaveUsers = function() { var request = $http({ method: "post", url: '/Home/Index', data: {user: $scope.user } }); request.success(function() { alert('success'); }); request.error(function() { alter('failed'); }); }; };

My C# code

[HttpPost] public ActionResult Index(List<Person> user) { List<Models.Person> data = new List<Models.Person>(); return null; } public class Person { public string Name {get;set;} public string Here {get;set;} }

解决方案

http requests to my MVC API. EDIT: Slowly discovering the problem, it has something to do with the the pushing of the data from form into the array. the error message I am getting back is "TypeError: Converting circular structure to JSON" I have a simple and I mean simple HTML form that allows me to collect 2 pieces of information and collect it into an Angularjs array and then pass it to the WEB API method. But my main problem is that If I spell the parameter incorrectly if calls the method and the value of the input parameter is blank. If I spell the parameter correctly it doesn't call the method at all. any pointer to help me learn what is going on? Thanks Simon

<div ng-app="Demo"> <div ng-controller="MyController"> <input type="text" id="username" name="username" placeholder="name..." ng-model="user.name"/> <input type="text" id="otherdetails" name="otherdetails" placeholder="name..." ng-model="user.name"/> <button type="button" ng-click="Add(user)">Save</button> <button type="button" ng-click="SaveUsers()">Save To Web API</button> </div> </div>

My Angular code is as follows

angular.module('Demo', []); angular.modeul('Demo').controller('ButtonCtrl', function(

scope,

http) {

更多推荐

角度发布到MVC Web API问题

本文发布于:2023-10-12 01:21:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:角度   MVC   API   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!