Incorporate JSON data using jQuery's AJAX in MVC3

I need assistance with parsing the JSON data retrieved from a webservice through my controller. Currently, I am displaying the entire JSON string in a div as text. However, I only want to extract specific values such as "color" and "size". I am unsure of the terminology and process involved in this task, whether it needs to be done within the controller or JavaScript. Any guidance on object arrays, deserialization, etc., would be greatly appreciated.

$.ajax({
    url: this.href,
    type: 'GET',
    dataType: "json",
    data: { myPartNo: returnVal },
    success: function (result) { 
        ShowJson(result);
    }
});

Answer №1

When working on the backend, it is common to create a DTO (data transfer object) that contains all the necessary information:

public class MyDataObject
{
    public string value {get; set;}
    public string color {get; set;}
    public int size { get; set; }
}

In the controller, you can simply convert this object into JSON format:

ActionResult MyController(int someParameter)
{
    MyDataObject model = new MyDataObject();
    model.value = ...
    return this.Json(model);
}

On the frontend, you can then process the returned JSON data as an ordinary object:

ShowJson(result.color);

//or

$("#mydiv").css("color", result.color); // for example

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

Tips for displaying user-entered information from a form after submitting it with PHP

How can I display the email in the email text input field with this code? It doesn't seem to work correctly when there is a failed login attempt. value= "if(isset($_POST['submit'])){ ?PHP echo $_POST[''email']?>"; ...

My current issue lies in my ability to successfully retrieve data from the controller using AJAX calls in .NET CORE, however, I am

Trying to pass a string id from the View to the controller for calculations and then return a string back to the view. Although I can receive the return data from the controller, the parameter 'String data' in the action is always null, indicatin ...

Tallying the Number of Accordion Toggle Clicks

Let me present a scenario where I have some accordions and would like to keep track of how many times the user expands each one. Could you guide me on how to implement this particular feature? Appreciate your support, Kevin ...

Submitting JSON through JQuery to a Spring Roo Handler

Encountered an issue with using JQuery and Spring Roo recently. Wanted to post data to a JSON enabled URL of a Spring Roo controller. The controller worked fine when posting data via Curl command: curl -i -X POST -H "Content-Type:application/json" http:// ...

The React component fails to render on the screen

Upon retrieving data from the database server, attempts to render it result in the data being shown in the console log but not displayed in the component. What could be causing this issue? useEffect(() => { readRequest().then(setTodos); c ...

Reorganize divisions using Bootstrap

I am exploring different ways to manage responsiveness through the reordering of divs. While I am aiming for a solution that is highly flexible, any suggestion would be appreciated. Desktop View: https://i.stack.imgur.com/boSOa.png Mobile View: https:// ...

Using setInterval on a batch of freshly generated div elements

I am interested in creating a small website where I can display multiple clocks for various time zones. However, I have encountered an issue with the setInterval function. Below is the code snippet: function addClock () { $("#container").append('& ...

issues with jquery progress bar not updating value

How can I display two progress bars with the same value specified in the data attribute? Here is the HTML code: <div> <div class="p" data-value="54"></div> </div> <div> <div class="p" data-value="45"></div> < ...

Understanding how to utilize jQuery or prototype to interpret onclick event containing "setLocation(...)" can enhance the functionality

I am dealing with a specific tag: <button onclick="setLocation('http://mysite.com/checkout/cart/add/product/17/')" /> My goal is to trigger an ajax request when the button is clicked. I want to extract the URL segment "product/17" and app ...

Is a function repeatedly invoked?

I have implemented a Pagination component in NextJS as follows: import Pagination from 'react-bootstrap/Pagination'; import Router from "next/router"; import {useEffect} from "react"; export { Paging } function Paging(props) ...

Unexpectedly, optimization causing issues on Angular site without explanation

Currently, I am utilizing Angular to construct a front-end website that searches for and showcases information obtained through API requests. Whenever I compile the project into a file bundle for static deployment using ng build, I notice that the resultin ...

What is the method for sending an object to a concealed form within php?

I am faced with a challenge involving an object named $todovalues and a form. The task at hand is to pass the entire todovalues object through the form. Here is how my form is structured - <form action="" method="post"> <input class="todobuttons ...

Is there a way to monitor and trigger a function in jQuery when a loaded PHP file is modified?

I am currently working on a dynamic dashboard that automatically updates every few seconds to display new information fetched from a PHP file. My goal is to trigger an alert only when there is a change in the data itself, rather than just a refresh. In ord ...

In TypeScript, the function is failing to retrieve the complete array value

I'm facing an issue with a program that is supposed to piece together all the letters, but it's not functioning correctly. I've tried using the debugger, but it doesn't display any errors. Here's the code snippet: var phrase = [ &a ...

I am attempting to link my Firebase real-time database with Cloud Firestore, but I am encountering import errors in the process

I am currently working on enhancing the online functionality of my chat app by implementing a presence system using Firebase Realtime Database. Here is the code snippet that I have created for this purpose: db refers to Firestore and dbt refers to the Rea ...

Is there a way to display the HTML input date by simply clicking on text or an image?

I need to display a date picker when I click on specific text or an image. I am working with reactjs and utilizing the HTML input type="date", rather than the React-datepicker library. Here is the code snippet: import { useRef } from "r ...

observing calls made with twilio using javascript

My goal is to track the status of a phone call happening between two people using their phone numbers. Let's say +558512344321 and +558543211234 are currently on a call. As soon as the call begins, I want the browser to display "Call in progress," and ...

What methods can be used to include optional settings in a custom JsonDotNetResult?

Presented here is an alternative JsonResult, known as the class below, which offers a more advanced JSON serializer compared to the default option for MVC Web Apps (unnecessary details are excluded): public class JsonDotNetResult : JsonResult { public ...

The mouseup event fails to trigger upon dropping a component with React-dnd

Currently, I am working on a project that requires drag and drop functionality using the React-dnd package. While I have been able to successfully implement this feature, I am facing an issue with the target component where draggable items are supposed to ...

Make sure that the iframe loads the next page with enough force to break out

My dilemma involves an iframe that loads the new tab page. When a user clicks on the thumbnail, it opens within the iframe. My goal is to have any subsequent load in the iframe redirected to window.top. Is there a way to achieve this without manually setti ...