Javascript 从C#应用程序向角度页面发送post请求
我知道如何使用angular向API发送post请求,但不知道如何检索从应用程序发送的post请求 假设我有一个C#post请求:Javascript 从C#应用程序向角度页面发送post请求,javascript,c#,angularjs,http,post,Javascript,C#,Angularjs,Http,Post,我知道如何使用angular向API发送post请求,但不知道如何检索从应用程序发送的post请求 假设我有一个C#post请求: public string sendPost(string data) { // Create a request using a URL that can receive a post. WebRequest request = WebRequest.Create("http://localhost:5000/ap
public string sendPost(string data)
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("http://localhost:5000/app/edit");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "{\"data\":\"" + data + "\"}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/json";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
在我的angular应用程序中,我有一个控制器:
(function () {
'use strict';
angular.module('anbud')
.controller('editController', ['$scope', 'localStorage', 'md5', '$routeParams', 'httpError',
function ($scope, localStorage, md5, $routeParams, httpError) {
function processHeaderData() {
console.log($routeParams.json);
}
processHeaderData();
}]);
}());
我可以通过url发送数据并使用routeParams,但我想使用POST。如何访问POST数据 我认为你把前端技术和web服务器技术混为一谈了 Angular是一种前端技术,您的意思是向Angular发送帖子,实际上意味着向托管Angular网站的web服务器发送请求 一旦明确了这一点,您只需要向web服务器发送一个API调用,就像处理angular一样,服务器将返回您需要的json数据
如果您想获得特定路由的HTML表示,恐怕没有好的方法来实现这一点。这对于angular也不特别,任何使用ajax调用加载数据的站点都会遇到这个问题。angular$http.post也可以检索数据。出于安全目的,这是非常有益的。这方面的逻辑仍然是一样的 您可以通过以下方式从c#controller发送数据:
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Sample.API.Controllers
{
public class ContentDataController
{
[Route("api/ContentData")]
[HttpPost]
public HttpResponseMessage SamplePost(SampleRequest request)
{
HttpResponseMessage response;
try
{
var cda = dataContentAdapter();
//Business logic here
var result = cda.GetContent(request);
//pass the above result to angular
response = Request.CreateResponse(HttpStatusCode.OK, result);
response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
}
catch (Exception ex)
{
ApplicationLogger.LogCompleteException(ex, "ContentDataController", "Post");
HttpError myCustomError = new HttpError(ex.Message) { { "IsSuccess", false } };
return Request.CreateErrorResponse(HttpStatusCode.OK, myCustomError);
}
return response;
}
}
}
这样,从angular发送一个请求,其中包含请求中需要使用的任何数据。最好是发送json,在业务逻辑中解析json
使用新的有效负载为angular$http创建一个HttpResponseMessage
,以便接收