I am able to access the JSON file from the URL without any issues, but for some reason, I am unable to fetch it using $

(function(){
$(document).ready(function(){
    $.getJSON('http://dev.markitondemand.com/MODApis/Api/v2/Quote/json?symbol=AAPL&callback=?', function(){console.log("function was run");});
  });
}())

I recently delved into working with APIs. However, I am facing an issue that I can't seem to solve. When I paste the URL in my browser, the JSON data loads perfectly fine. But when I remove the '&callback=?' part at the end of the URL (which is what I would do for regular JSON), I encounter the following error message:

XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I need to resolve this issue urgently as it is related to a coding camp application due by the weekend. If the technical details are too complex for me to grasp at my current level, I would greatly appreciate simple instructions on how to retrieve the JSON file from their server and convert it into a usable object in Javascript. Thank you for your assistance :)

Answer №1

It seems that you are using JSON format for the API, but it should actually be JSONP. Check out the API documentation.

To correct this issue, simply replace:

v2/Quote/json?

with:

v2/Quote/jsonp?

Take a look at the working demo below:

(function(){
$(document).ready(function(){
    var url = 'http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=AAPL&callback=?';
    $.getJSON(url, function( data ) {
        document.body.innerHTML = '<pre>' + JSON.stringify( data, null, '\t' ) + '</pre>';
    } );
  });
}())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Answer №2

It seems like the issue is related to Cross-Origin Resource Sharing (CORS). If you have the authority over this API, simply add the following line of code:

header('Access-Control-Allow-Origin: *');

If you do not have control over the API, you can access it using the following link:

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 on transferring the id value from a modal window to an ajax click event?

I'm having trouble passing the image ID to AJAX for updating it in a modal window. After opening the modal and grabbing the correct ID, I attempt to pass it to AJAX but it shows as undefined. How can I properly pass the ID for further processing? $( ...

Working with JSON_TABLE and CLOB larger than 32k

I have encountered an issue while trying to read and parse JSON code. Everything seems to be working fine except when the JSON string exceeds 32k characters - in such cases, the import of the JSON string fails. I am currently using the following code snipp ...

Generating a JSON data set with two separate pieces of data, not contained within a dictionary

Within my application, there exists an HTML table that is dynamically generated using the following code: function generateTableRow() { var emptyColumn = document.createElement('tr'); emptyColumn.className ='data'; emptyColumn.inne ...

IntelliJ coverage for backend JavaScript

Is it possible to analyze code coverage in IntelliJ without using a browser? http://www.jetbrains.com/webstorm/webhelp/monitoring-code-coverage-for-javascript.html Though there are tutorials by JetBrains on code coverage, they all seem to require a browse ...

The functionality of jQuery's toggleClass is not functioning properly when clicking on a different thumbnail

My objective: I want to display a set of thumbnails. When a thumbnail is clicked, I want to toggle the hiddenElement class and show a large image inside a hidden div. If anywhere in the DOM is clicked, it should hide the current image or allow the user t ...

Content not being displayed in Rails 4 with AJAX using Bootstrap 3 Modal

Despite researching several resources on BS modals with AJAX, such as the one found here: Bootstrap Modal in Rails Keeps displaying the first record, I am still encountering issues. Upon implementing AJAX (using JQuery 2.1.1), the modal successfully opens ...

Tips to prevent the return of undefined when a condition is not satisfied

I'm currently working on a react app and I have an array of objects (items) that I want to iterate over in order to display each object based on its index. I am using useState to set the index, which is updated when the user clicks a button. const ...

Incorporating images with JavaScript in Jade templates

Currently I have a layout.jade code that I am looking to modify the javascript for. My goal is to load an image onto the page every time it is reloaded using jade. Here is the existing code: html head title= title script(src='/libs/jquery/j ...

How can I efficiently transfer information between AngularJS modules?

Angular offers the flexibility of creating independent Modules that can be reused in various parts of your application. Imagine having a module dedicated to managing lists, which you want to use across your entire application and populate in different ways ...

Leveraging jQuery in Content Scripts for Chrome Extensions

I am currently working on developing a Chrome extension that will prompt a small input whenever a user highlights text on a webpage (similar to Medium's feature that allows you to tweet highlighted text). While I am making progress, I believe using j ...

Using jQuery, is there a way to move an element from one div to another, but only if the destination div is currently empty

<div id="apply"></div> <div id="droppable_slots"> <div class="slot 1">1</div> <div class="slot 2">2</div> <div class="slot 3">3</div> </div> Is there a way to utilize jquery in order ...

Tips for avoiding event listeners from being triggered multiple times

Implemented an event listener on an HTML element that was retrieved by className within the render method of highcharts, but for some reason, the listener is being triggered twice. The issue seems to be with the onClick handler in the highchart's rend ...

An Error Code 400 is Triggered When Attempting to Update Text Field in SharePoint List Using Rest API

I need help with updating a field in my SharePoint list named "Search Results". Specifically, I am trying to change the value of the "Users" field to "Test" for a particular record ID. $(window).load(function(){ try{ ...

Utilizing the power of JDBC in conjunction with Elasticsearch and the Json data type in Post

Database Version: Postgresql 9.3.2 Elasticsearch Version: 0.90 Elasticsearch River JDBC Version: 2.2.2 Postgresql JDBC Version: 9.3-1100 JDBC 41 I am attempting to transfer a postgresql Json data type column into elasticsearch using the elasticsearch ...

Assistance with jQuery in Javascript is needed

Currently, I am in search of an effective vertical text scroller. My desired scroller would move vertically in a continuous manner, ensuring there is never any empty space while waiting for the next text to appear. I am open to using either JavaScript or ...

The worth of the scope is evident, yet it remains undefined when trying to access it

Currently, I am in the process of developing an AngularJS directive. However, I have encountered an issue where a scope variable appears to be undefined when attempting to access it. Interestingly, upon printing out the scope, it is evident that the variab ...

Issue retrieving parameter through Ajax in MVC4

I am currently working on an Ajax call to a controller in MVC4 where I need to pass the name of a file that is going to be deleted. As someone new to MVC4 and also Ajax, I am unsure how to retrieve this value once inside my controller. Despite setting up ...

Looking to automatically scroll to the bottom by clicking on text using javascript/jquery?

I am currently working on a website project where I have a specific requirement. I need the webpage to scroll towards the bottom when a particular text is clicked, using JavaScript/jQuery. <p class="ml-2 mb-2 d-flex view_profile_options"><a hre ...

Updating a div element dynamically using AJAX in DJANGO

Currently in the process of developing a chat application. Using jquery $.post() to add chat messages has been successful thus far. My next step is to fetch the most recent chat message from the table and update the list on the chat page. As a beginner in ...

How to target a class in jQuery that contains two specific strings

There are multiple images in my HTML, each assigned two classes. Here's a snippet of the relevant code: class = "thing other0-a" class = "thing other1-a" class = "thing other2-a" class = "thing other3-a" class = ...