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 8264285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:24:01+00:00 2026-06-08T04:24:01+00:00

I’m trying to get a JSON input with AJAX and load it in a

  • 0

I’m trying to get a JSON input with AJAX and load it in a select control.
but when I run it :\ It stuck on “Downloading the recipes….”.
anyone see the problem maybe? (I tried couple of changes but nothing work so far)

1 more issue can anyone think on a shorter way to do the

ConvertToTable(targetNode)

cuse it’s way to long and complex, I think :\

    <script type="text/javascript">
             function loadXMLDoc() {
                 var xmlhttp;
                 document.getElementById("span").style.visibility = "visible";
                 document.getElementById("span1").style.visibility = "hidden";
                 document.getElementById("button").style.visibility = "hidden";
                 xmlhttp = new XMLHttpRequest();
                 xmlhttp.onreadystatechange = function () {
                     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                         result = xmlhttp.responseText;
                         result = eval('(' + result + ')');
                         txt = "<select onchange='ShowRecipeDetails(this)'>";
                         for (i = 0; i < result.length; i++) {
                             txt = txt + "<option VALUE=" + result[i].recipe + ">" + result[i].recipe + "</option>";
                         }
                         txt = txt + "</select  >";
                         document.getElementById("span").style.visibility = "hidden";
                         document.getElementById("span1").style.visibility = "visible";
                         document.getElementById("myDiv").innerHTML = txt;
                     }
                 }
                 xmlhttp.open("POST", "http://food.cs50.net/api/1.3/menus?meal=BREAKFAST&sdt=2011-03-21&output=json", true);
                 xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                 xmlhttp.send();
             }
             function ShowRecipeDetails(event) {
                 // get the index of the selected option 
                 var idx = event.selectedIndex;
                 // get the value of the selected option 
                 var field = event.options[idx].value;
                 $.ajax({
                     type: "GET",
                     url: "http://food.cs50.net/api/1.3/recipes?&output=json&id=" + field,
                     success: function (data) {
                         $("#TableDiv").html(ConvertToTable(data[0]));
                     }
                 });
             }
             function ConvertToTable(targetNode) {
                 var table = "<table border = 1 borderColor =green>";
                 table += "<tr>";
                 table += "<td>" + "ID" + "</td>";
                 table += "<td>" + targetNode.id + "</td>";
                 table += "</tr>";
                 table += "<td>" + "Ingredients:" + "</td>";
                 table += "<td>" + targetNode.ingredients + "</td>";
                 table += "</tr>";
                 table += "<td>" + "Name:" + "</td>";
                 table += "<td>" + targetNode.name + "</td>";
                 table += "</tr>";
                 table += "<td>" + "Size:" + "</td>";
                 table += "<td>" + targetNode.size + "</td>";
                 table += "</tr>";
                 table += "<td>" + "Unit" + "</td>";
                 table += "<td>" + targetNode.unit + "</td>";
                 table += "</tr>";
                 table += "<td>" + "VEGETARIAN:" + "</td>";
                 table += "<td>" + targetNode.VEGETARIAN + "</td>";
                 table += "</tr>";
                 table += "</tr>";
                 table += "</table>"
                 return table;
             }
 </script>

and the HTML:

<button id="button"  type="button" onclick="loadXMLDoc()" >Get all recipes</button>
<br />
<span id="span" style="visibility:hidden">Downloading the recipes....</span>
<span id="span1" style="visibility:hidden">Please choose a recipe ID to view</span>
<div id="jsonDiv"></div>
<div id="myDiv"></div>
<div id="TableDiv"></div>
  • 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-08T04:24:04+00:00Added an answer on June 8, 2026 at 4:24 am

    The HarvardFood API also supplies a JSONP version. So if you change your URL to this:

    http://food.cs50.net/api/1.3/menus?meal=BREAKFAST&sdt=2011-03-21&output=jsonp&callback=parseResponse
    

    you can create a parseResponse function to handle the data that comes back, and you can do the AJAX by inserting a script tag.

    The problem is that you’re running afoul the Same Origin Policy.


    I see that you’ve updated the question to use jQuery AJAX. That offers a jsonp data type, which might be easier than adding a script tag.

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

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
I need to clean up various Word 'smart' characters in user input, including but
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words

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.