Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8194773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:56:03+00:00 2026-06-07T04:56:03+00:00

I have a simple page that takes an ID to find a specific product

  • 0

I have a simple page that takes an ID to find a specific product from the List of Products returned from the database and then display this product in the html page. I am able to see the JSON I expect to see when I use /api/products/12345 but when I try and query the data from the Index.cshtml page I get back the result –> undefined: $undefined in my page. I will past the Product class and my html page. Note, the display for ALL products renders perfectly.

public class Product
{
    public int ID { get; set; }
    public string ProductDescription { get; set; }
    public string UnitOfMeasure { get; set; }
    public decimal MSRP { get; set; }
    public string Category { get; set; }
    public int CategoryID { get; set; }
    public string ZipCode { get; set; }
}

This is my Index.cshtml page

<html lang="en">
<head>
<title>.:: Web API ::.</title>
<script src="../../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        // Send an AJAX request - the second parameter is a callback function that is invoked when the request successfully completes.
        $.getJSON("api/products/",
        function (data) {
            // On success, 'data' contains a list of products.
            $.each(data, function (key, val) {

                // Format the text to display.
                var str = val.ProductDescription + ': $' + val.MSRP;

                // Add a list item for the product.
                $('<li/>', { html: str }).appendTo($('#products'));
            });
        });
    });

    function find() {
        var id = $('#prodId').val();
        // Again, we call the jQuery getJSON function to send the AJAX request, but this time we use the ID to construct the request URI.
        $.getJSON("api/products/" + id,
            function (data) {
                // On success, 'data' contains a list of products.
                $.each(data, function (key, val) {

                    // Format the text to display.
                    var str = val.ProductDescription + ': $' + val.MSRP;
                    $('#products').html(str);
                });
            })
        .fail(
            function (jqXHR, textStatus, err) {
                $('#products').html('Error: ' + err);
            });
        }     
</script>
</head>
<body>
<div>
    <h1>All Products</h1>
    <ul id='products' />
</div>
<div>
    <label for="prodId">ID:</label>
    <input type="text" id="prodId" size="5"/>
    <input type="button" value="Search" onclick="find();" />
    <p id="product" />
</div>

The problem I am having is with the find() function rendering to the UI meaningful data, I am seeing that data matching the ID 12345 is returned successfully.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:56:04+00:00Added an answer on June 7, 2026 at 4:56 am

    Without seeing the code, it’s hard to know for sure, but I’m guessing the code of the controller actions at api/products it returns an array of objects as JSON – probably something like this: [{ID: 5, MSRP: 20.10},{ID: 6, MSRP: 10.10}]. Therefore using $.each(data, function() {...}) to go through the list of items makes sense.

    However, I’m guessing that api/products/5 returns a single object as JSON – something like this: {ID: 5, MSRP: 20.10}. Without the wrapping array, $.each(data, function() {...}) won’t do what you want. Try this for your callback function within find():

    function (data) {
        // On success, 'data' contains a *single* product.
       var str = data.ProductDescription + ': $' + data.MSRP;
       $('#products').html(str);
    })
    

    If that doesn’t work, could you show a snippet of the data being returned by api/products vs. the data returned by api/products/5?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple php page that displays data from a mysql database. I
I have an extremely simple page that I'm trying to view in the Facebook
I have a simple django page that has a counter on it. I use
I am simply learing Ajax with jQuery and have a simple page method that
This is bugging me ... I have a simple HTML page that has a
I have a simple page here to demonstrate the issue that I am seeing.
If I have a simple web page and script that looks like this: <body>
I have a simple jQuery code that runs on a web page that has
Here's the code for a simple page I have that is not working: <head>
I have a simple iPhone application that is very similar to the Page Control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.