Using JavaScript to make an HTTP request for a JSON object from an API hosted on a local server

I currently have an API running on my local machine using Sinatra and JRuby to interact with a SQL server. When I access 'localhost:4567/get/233310/loc', it returns a JSON object.

[{"uid":233307,"lat":41.4191113333333,"long":-72.8941905}]

My goal is to retrieve this JSON object and assign each value to a variable in my JavaScript code. Do you think the best approach would be through an HTTP request, or is there a better way?

In the future, I aim to automate this request every 30 seconds, but that's a task for later on.

Can someone assist me with this process? Your help is greatly appreciated.

Answer №1

There is another fantastic library known as Prototype that you may find helpful. Take a look at it here:

Incorporating the prototype.js library into your page to make a request is quite simple. Just include the following code:

<script src="prototype.js"></script>

Afterwards, execute this code:

var url = 'http://localhost:4567/get/233310/loc'; new Ajax.Request(url, { method: 'get', onSuccess: function(transport) { var json = transport.responseJSON; window.alert(json.inspect()); } });

Once this process is finished, you will receive a popup alert displaying the contents of your JSON object.

Ensure that your server-side script delivers the JSON with the HTTP header "Content-Type" set to "application/json" so the client-side script knows to parse it as JSON.

We hope you find this information valuable.

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 preventing the need to create a function within a loop

Currently, I am in the process of collecting data through REST calls. I have created a function that accesses a "directory" endpoint to retrieve a list of individuals. While I can easily obtain details about their children, I need to make individual AP ...

Utilizing Jquery to locate the precise HTML element or content that is positioned at a specific pixel location on the

I am trying to track down the exact HTML element or content occupying a specific pixel position on a web page. I am currently using jQuery and the scrollTop() method like this: $(window).scroll(function (event) { var scroll = $(window).scrollTop(); ...

If I do not utilize v-model within computed, then computed will not provide a value

I'm fairly new to coding in JS and Vue.js. I've been attempting to create a dynamic search input feature that filters an array of objects fetched from my API based on user input. The strange issue I'm coming across is that the computed metho ...

Retrieve the text value of a selected option in a v-select component

After reading some documentation about v-select and slots, I am a bit confused about whether it can be applied to my specific scenario on this codepen. All I really need is to capture the selected text (not the value) and utilize it somewhere in the code: ...

add an image from a URL to a json document

I'm trying to add a picture to each entry in my books.json list. The format of my books.json list is like this: [ {"id": 1,"cover": "link1.jpg","bookTitle": "X", "author": "X"}, ...

Tips for creating a horizontal list within a collapsible card

When a user clicks on a button, I am dynamically populating a list of items. Despite using list-group-horizontal, I am unable to make it display horizontally. HTML code <div id="textarea_display" class="mt-2 "> <label&g ...

Trouble arises when attempting to utilize the WCF restful service through an Ajax call

I've created a WCF restful service that has a login method exposed to the outside world. The purpose of this method is to validate user credentials with a database (specifically the login table) and return the result in json format. C# code: [Operat ...

JQuery is unable to initiate a keyup event

I am currently utilizing jQuery in a web application. On one of my pages, I have set up an event listener for keypresses as shown below: document.addEventListener('keyup', function (event) { event.preventDefault(); var key = event.k ...

What Causes the Undefined Value of "this" in Vue 3 Functions?

Here is a basic example of a component in Vue 3: <script> import { defineComponent } from 'vue' export default defineComponent({ name: 'Test', setup(){ return{ one, two } } }) function one(){ console. ...

Is the server delivering an unexpected response?

Implementing a post request using the fetch API in React JS, I provide a URL to the server from the frontend. The server then utilizes this URL to make an API call to Clarifai API. The expected response is received from the API; however, when transferring ...

What is the code in CodeIgniter to retrieve the string 'Sugar & Jaggery, Salt' using <a>?

Can someone please help me? I am a beginner in CodeIgniter and I am having trouble passing a URL with a string. The controller is not accepting the string as expected. How can I fix this issue? //Below is the HTML code for passing a string value index.ph ...

Issue with Google Finance JSON response not functioning as expected in Chrome and Firefox browsers, yet appears to be working properly in

I am currently working on a JavaScript project that involves fetching STOCK data from the Google Finance API. When I manually paste the link into my browser, I can successfully retrieve the JSON response: // [ { "id": "22144" ,"t" : "AAPL" ,"e" : "NASDAQ ...

Is there a way to eliminate the border of an image attribute pulled from an input field?

Seeking assistance with a persistent issue I'm facing. I have an input for an image and a script to display the selected image. However, when the input is empty, a frustrating black border appears around the image attribute. How can I remove this bord ...

What is the function of the next and back buttons in AngularJS?

I need assistance with creating next and previous buttons in Angular. As I am new to programming, I have written a program that works when using a custom array $scope.data = []. However, it doesn't work when I use $http and I would appreciate any help ...

Preventing form submission with JavaScript by implementing multiple validation checks

Preventing a form from submitting when validation returns false is possible. Here's an example: <form name="registration" action="registration.php" onsubmit="return validate()"> <!-- some inputs like: --> <input type="text" id="us ...

Django compressor throwing minification hiccup

I'm currently utilizing a tool for automatic minification known as django compressor. Unfortunately, it seems that the minification process with django compressor is causing errors to be introduced. Updated script with semicolons: Before: var ap ...

Providing parameters to a function. What could be the issue?

Here is a link to my code on jsFiddle I am attempting to pass an argument to a function, but it seems like the argument is not being received or executed. <a href="javascript:addRemove('7249');">Details</a> This code snippet uses J ...

The error message "Unable to map props.theTodos" is displayed

I'm facing an error message while compiling: "TypeError: props.theTodos.map is not a function". I've been struggling with this issue for quite some time, but I haven't found a solution yet as I'm using hooks instead of class components. ...

Removing a Button from a Form Using JavaScript

Here is a snippet of my HTML code: <form id="form"> <input id="deleteNumber" name="del" type="hidden" /> <input id="addAddress" name="addAddress" type="hidden" /> ... ... ... <a href="javascript:deleteAddress();" class="deleteItem" ...

How can I customize the styling of Autocomplete chips in MUI ReactJS?

Trying to customize the color of the MUI Autocomplete component based on specific conditions, but struggling to find a solution. Any ideas? https://i.stack.imgur.com/50Ppk.png ...