Tips for accessing and using class-stored parameters in a function

I have a WebApi method that accepts parameters stored in a class.

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

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


    }

This is my method:

public HttpResponseMessage RetrieveData(MyParameters inputParams)
    {

        //Logic goes here

    }

Now, I need to send these parameters using a JavaScript file and XMLHttpRequest. Here's what I have:

 var xhr = new XMLHttpRequest();
        xhr.open("POST", "./api/RetrieveData");
        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, "SavedFile.zip");
            }
        };
        xhr.send();

How can I pass the parameters to this request?

Answer №1

After some experimentation, I finally cracked the code! Here's how I crafted an object in JavaScript:

const myData = {
    Name: "Test",
    Age: "33"
};

Once the object was created, I converted it to a string and sent it via XMLHttpRequest like so:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the issue with my css `content:` not functioning across different browsers, and how can I enhance cross-browser compatibility in all cases

Working on my first HTML/CSS project, I encountered a browser compatibility challenge. The code functions well in Firefox, IE, and Edge but fails in Chrome. Specifically, I am trying to make a button display alternating up and down arrows when clicked. Whi ...

When capturing photos using h5 on iOS devices, the browser may experience a flash back issue

I have been searching Baidu and Google for a while now, but I still haven't found a solution. Here is the specific issue: When using h5 to take photos on iOS systems, the browser flashes back. HTML Code <img src="xxx" onclick="sta ...

Express Js: Implementing Dynamic Data Loading on Scroll

We are currently facing an issue where the dynamic data from Mongo db is loading all at once instead of through scrolling. Our objective is to have the data load progressively as we scroll, all on one single page. // We attempted using Lazy loading carous ...

Tips for extracting valuable insights from console.log()

I'm currently utilizing OpenLayers and jQuery to map out a GeoJson file containing various features and their properties. My objective is to extract the list of properties associated with a specific feature called "my_feature". In an attempt to achi ...

React Drag and Drop: Div Resisting Dragging

I recently attempted to create a drag and drop list feature in a React app without using any external packages. I came across a tutorial that claimed to achieve this. This is the code I implemented: class App extends React.Component { state = { ite ...

What are the steps for generating a diagram using Chart.js?

I'm attempting to use Chart.js to create a specific diagram. Current Challenges: The point on the x-axis should be centered within the categories. I would like the value to appear above the point. https://i.sstatic.net/FFFr1.png This is what my co ...

Maximizing the full potential of a JavaScript menu using WebDriver

I am facing a challenge in clicking a button on a website using WebDriver that utilizes JavaScript to expand a menu and display additional links: <div id="menu"> <div id="security_bt" class="advanced_white_close_button" onclick="open_or_close_sub ...

What could be causing checked="checked" to fail validation in JavaScript?

I'm feeling a bit puzzled about this issue. Here's the HTML code I have - <input type="radio" id="20577091" name="q_engine" value="20577091" style="position: absolute; opacity:0;" checked="checked" /> and here is the corresponding JavaScr ...

React Native: LogBox now including all warnings

Looking for a way to use LogBox to filter out specific log messages, but despite my attempts to ignore them, they still show up in the console... "react-native": "0.68.2", In my main file (index.js), this is how I've tried to impl ...

Issue with retrieving data during a loop in Javascript

Whenever I try to loop through multiple values from a database and click on a specific item in the menu, JavaScript only captures the first value from the database. Even if the function is inside the loop and I click on the last item, it still displays t ...

What is the best way to implement ng-change within an ng-repeat?

My goal is to trigger a function every time a check box is clicked in ng-change. I am looking for a way to determine the value of the model or identify which checkbox was clicked, so that I can call another function based on the checked value. Additionally ...

Getting the Angular component class reference within a triggered Highcharts selection event callback - what's the best approach?

It seems like I'm facing a common javascript closure issue, but let me illustrate it with a specific example as I'm struggling to grasp it in an Angular context. In my Angular component, I'm using the Highcharts library to visualize data. W ...

Saving asp.net strings in different formats to datetime fields in a database

Can someone please assist me with saving the string value of textbox to the database as a datetime, and for updating/editing purposes, display it again in the textbox as a string? The reason for this is that the datetime input will be copied from Outlook a ...

Passing data between Express controllers using Socket.io

Looking for guidance on using Node.js / Express for my AngularJS backend. I've searched everywhere for help, but can't figure out how to implement the suggestions in my code. Here's how my application functions: A Scala process sends log ...

Parsing a text file containing various data entries, converting them into objects, and then storing them in an array

I need assistance with parsing and storing information from a file containing multiple DNA sequence ID's and their sequences. My goal is to create an object for each entry: function processSequences(event) { //Retrieve the file from the HTML inpu ...

How can one easily remove the parent directory from a path when working with Node.js?

If the path is /foo/bar/baz.json and I need only /bar/baz.json, how can this be achieved using Node.js's path functionality? ...

What causes the inconsistency in TypeScript's structure typing?

It is well-known that TypeScript applies structure typing, as demonstrated in the following example: interface Vector { x: number; y: number; } interface NamedVector { x: number; y: number; name: string; } function calculateLength(v: Vecto ...

Error encountered while attempting to import a Bootstrap JavaScript file in webpack

I am currently working on a Gridsome project (v0.7.23) where I have loaded the Bootstrap framework via npm. In this project, I am using node v14.18.0 through nvm. However, when attempting to import a Bootstrap JS component (specifically 'collapse&apo ...

Exploring React and finding the way to a specific item

I have a list of users displayed in a table. When a user clicks on a row, I want to navigate to a different component to show the details. <tbody> {this.state.clients.map(client => ( <tr className="tableRow" onClick={() => this. ...

Utilizing the jQuery Serialize Method in Your Codebase

After users choose specific facets to narrow down their search query, they often bookmark the page. In order for the refinements panel to include these selected facets in the URL each time a facet is added or removed, serialization capability is required. ...