角度发布到MVC Web API问题 [英] angular posting to an MVC Web API problem
问题描述
我想学习角度。我目前正在学习 $ http
对我的MVC API的请求。
编辑:慢慢发现问题,它与将数据从表单推送到数组有关。我得到的错误信息是TypeError:将循环结构转换为JSON
我有一个简单的,我的意思是简单的HTML表单,可以让我收集2件信息并将其收集到Angularjs数组中,然后将其传递给WEB API方法。
但我的主要问题是,如果我调用参数并且输入参数的值为空,则拼写参数不正确。如果我正确拼写参数,它根本不会调用该方法。任何帮助我了解正在发生的事情的指针?
谢谢
Simon
< div ng-app = 演示 >
< div ng-controller = MyController >
< ; 输入 type < span class =code-keyword> = text id = username 名称 = 用户名 占位符 = name ... ng-model = user.name / >
< 输入 type = text id = otherdetails name = otherdetails 占位符 = name ... ng-model = user.name / >
< 按钮 类型 = 按钮 ng-click = 添加(用户) > 保存< / button >
< < span class =code-leadattribute> 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 < span class =code-keyword> = text id = username 名称 = 用户名 占位符 = name ... ng-model = user.name / >
< 输入 type = text id = otherdetails name = otherdetails 占位符 = name ... ng-model = user.name / >
< 按钮 类型 = 按钮 ng-click = 添加(用户) > 保存< / button >
< < span class =code-leadattribute> button type = 按钮 ng -click = SaveUsers() > 保存到Web API < / button >
< / div >
< / div>
我的Angular代码如下
angular.module(' 演示',[]);
angular.modeul(' 演示')。controller(' ButtonCtrl', function (
< blockquote> 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问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!