【问题标题】:How to pass parameters which are stored in class如何传递存储在类中的参数
【发布时间】:2014-11-08 16:39:08
【问题描述】:

我正在使用 WebApi 并定义了接受存储在类中的参数的方法

 public class MyParameters
    {
        [DataMember(IsRequired = true, Name = "Name")]
        public string Name{ get; set; }

        [DataMember(IsRequired = true, Name = "Age")]
        public string Age{ get; set; }


    }

我的方法如下

public HttpResponseMessage GiveMeNames(MyParameters getParameters)
    {

        //My logic

    }

现在我想通过 javascript 文件传递​​这个参数,我正在使用 XMLHttpRequest

 var xhr = new XMLHttpRequest();
        xhr.open("POST", "./api/GiveMeNames");
        xhr.responseType = "arrayBuffer";
        xhr.setRequestHeader("Content-type", "application/zip");

        xhr.onload = function () {
            if (this.status === 200) {
                var blob = new Blob([this.response], { type: "application/zip" });

                window.navigator.msSaveBlob(blob, "SaveFile.zip");
            }
        };
        xhr.send();

如何将参数传递给此请求?

【问题讨论】:

  • 有人回答这个问题吗?

标签: javascript xmlhttprequest asp.net-web-api


【解决方案1】:

我想通了。我在 javascript 中创建了对象,如下所示

var myRequest =
         {
             Name: "Test", Age: "33"
         };

然后在请求中我将其字符串化并在 XMLHttpRequest 中发送

xhr.send(JSON.stringify(myRequest));

【讨论】:

    猜你喜欢
    • 2020-04-11
    • 1970-01-01
    • 2020-04-28
    • 2014-04-26
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    相关资源
    最近更新 更多