将数据从angulerjs客户端发布到WEBAPI控制器 [英] Post data from angulerjs client to WEBAPI controller

查看:59
本文介绍了将数据从angulerjs客户端发布到WEBAPI控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi,
am new in angular js  and webapi 
 
Problem:  
 
i cant post data using json url: "NewRoute/firstCall" from view to WebAPI controller 
 
but i can post using url: "http://localhost:2730/NewRoute/firstCall", 
 
who can i post data without  http://localhost:2730
 
thing is i got two projects in one solution.
1.AngularJS
2.WebAPI
 
 
In view part
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Scripts/angular.js"></script>
<script>
 
var TestCtrl = function ($scope, $http) {

$scope.SendData = function (Data) {
var GetAll = new Object();
GetAll.txt1 = Data.txt1;
$http({
url: "NewRoute/firstCall",
//url: "http://localhost:2730/NewRoute/firstCall",
dataType: 'json',
method: 'POST',
data: GetAll,
headers: {
"Content-Type": "application/json"
}
}).success(function (response) {
$scope.value = response;
})
.error(function (error) {
alert(error);
});
}
};
var myApp = angular.module("myApp", []);
myApp.controller("TestCtrl", TestCtrl);
</script>
</head>

<body ng-app="myApp">
<div ng-controller="TestCtrl">
<hr />
<div>
Enter text
<input type="text" placeholder="Enter your name" ng-model="details.txt1">
</div>
<hr />
<input type="button" ng-click="SendData(details)" value="Submit" />
<hr />
<h4 style="color:blueviolet">{{value}}</h4>
</div>
<hr />
</body>
</html>

 
 In Web Api controller
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApplication1.Controllers
{
[RoutePrefix("NewRoute")]
public class NewController : ApiController
{
public class GetAll
{
public string txt1 { get; set; }
}
[Route("firstCall")]
[HttpPost]
public string firstCall(HttpRequestMessage request,
[FromBody] GetAll getAll)
{
return "Success";
}
}
}
 
 
 
 please help me to solve this issue . thanks in advance





我的尝试:



i尝试了很多,但我是角度jS和webApi的新手,所以我无法得到正确的方法来获得结果



What I have tried:

i tried alot but am new to angular jS and webApi so i cant get proper way to get result

推荐答案

scope,


http){


scope.SendData = function(Data){
var GetAll = new Object ();
GetAll.txt1 = Data.txt1;
scope.SendData = function (Data) { var GetAll = new Object(); GetAll.txt1 = Data.txt1;


这篇关于将数据从angulerjs客户端发布到WEBAPI控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆