javascript - 从 C# 应用程序向 Angular 页面发送发布请求

标签 javascript c# angularjs http post

我知道如何向 API 发送带 Angular 发布请求,但想知道如何检索从应用程序发送的发布请求。

假设我有这个 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/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 应用程序中,我有这个 Controller :

(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 数据?

最佳答案

我认为您混淆了前端和网络服务器技术。

Angular 是一种前端技术,你所说的向 Angular 发送帖子,实际上是向托管 Angular 网站的 Web 服务器发送请求。

弄清楚这一点后,您只需像使用 Angular 一样向 Web 服务器发送 API 调用,服务器将返回您需要的 json 数据。

如果你想获得特定路由的 HTML 表示,恐怕没有什么好的方法可以实现。这也不是 Angular 特有的,任何使用 ajax 调用加载数据的站点都会有这个问题。

关于javascript - 从 C# 应用程序向 Angular 页面发送发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37067232/

相关文章:

javascript - 扩展帮助(后台运行时按键)

javascript - 如何在页面加载前使用 CSS 在响应式 slider 上设置高度

javascript - 在后退按钮上刷新 onsen ui 中的上一页

JavaScript、变量范围、变量求值顺序

C# HttpWebRequest 通过代理请求

javascript - 如何将列表作为参数传递给对话框?

javascript - 尝试为包含单词的特定 <tr> 设置 CSS 样式

javascript - 单击可重用函数只是 javascript

c# - Protobuf-net 中没有为类型 : System. Management.Automation.PSObject 定义序列化器

c# - 将 int(或任何值类型)存储为对象并将它们转换回 int(或值类型)是否有任何成本?