Retrieve JSON information from an external JavaScript source

Looking to utilize vanilla JavaScript to retrieve data from the MeetUp API. The code snippet being used on the website is as follows:

function addScript(src) {
    console.log(src);
    var s = document.createElement( 'script' );
    s.setAttribute( 'src', src );
    document.body.appendChild( s );
}

addScript('https://api.meetup.com/2/groups?callback=?&sign=true&member_id=8377069&page=20&api&key=API_KEY_GOES_HERE&only=name,link');

A script tag has been employed to access the data in order to circumvent cross-domain issues. While acknowledging jQuery could address this, I prefer not to rely on external libraries.

However, the provided code yields the following error:

Uncaught SyntaxError: Unexpected token :

This issue likely arises due to the JSON format of the return. How can this be resolved while still ensuring access to the JSON data? Your assistance is greatly appreciated.

For an example link, kindly refer to: http://jsfiddle.net/londonfed/7ms44ft6/

Answer №1

Utilizing the meetup API's callback functionality, you can create a global function within the window object and assign the callback parameter to the function name:

function loadScript(url) {
    console.log(url);
    var script = document.createElement('script');
    script.setAttribute('src', url);
    document.body.appendChild(script);
}

// Global callback function for handling data from api:
window.apiCallback = function(response) {
    // Process retrieved data as needed
    alert(response.results[0].name);
}
loadScript('https://api.meetup.com/2/groups?callback=apiCallback&sign=true&member_id=8377069&page=20&api&key=YOUR_API_KEY_HERE&only=name,link');

Answer №2

In order to effectively retrieve and process the data from a JSONP request, it is essential to include a callback parameter within your URL. This callback parameter should match the name of the function responsible for handling the returned data:

function handleJSONResponse(response) {
  console.log(response.items);
}

addScript('https://api.example.com/data?callback=handleJSONResponse&key=12345678&format=jsonp');

Keep in mind that the items property within the response object might require iteration through a loop.

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

Unexpected token N error was thrown while using the Jquery .ajax() function

While working on a form submission feature using JQuery .ajax() to save data into a MySQL database, I encountered an error message that says "SyntaxError: Unexpected token N". Can someone please explain what this means and provide guidance on how to fix it ...

Using Selenium webdriver to assign a JSON object to a paragraph element

What is the correct way to insert a JSON object into a p tag inside an iframe? I attempted the following approach but it displayed the text "[object Object]" rather than the actual content of the object... This is my implemented code: var arrJSON = [ ...

Using GraphQL to set default values in data within a useEffect hook can lead to never

Here's the code snippet that I'm working with: const [localState, setLocalState] = useState<StateType[]>([]); const { data = { attribute: [] }, loading } = useQuery<DataType>(QUERY, { variables: { id: client && client.id ...

I am facing difficulty in assigning a nested JSON received from a POST request to my class

I have a specialized WebAPI project that processes JSON data to create and populate new objects. The structure of my classes is as follows: public class User { public string email { get; set; } public string libraryId { get; set; } public ...

Tips for passing parameters in the $http GET request to transmit information from a dropdown menu using AngularJS

In the modal window I have for creating a new object, there are three forms: 1) a simple input to create "Name"; 2) A dropdown with "Types"; 3) A dropdown with "Ids". The issue arises when trying to send the data after filling out all the forms. An error o ...

Stop the npm start command with a specific error message if the file is not found in the repository

Can the npm start process be stopped or cancelled if a specific file is not found in your codebase? I am looking to end the process if the file dev-variables.js is missing, preferably displaying a custom error message like "You are missing the dev-variabl ...

Ways to remove any messages containing URLs that are not www.youtube.com or www.twitter.com

Recently, I have encountered a significant issue with Discord Scam Links in my server. I attempted the following approach: if(message.content.includes("discordscam.com")) { message.delete() } However, this method is not effective as it onl ...

Access the current page's URL using JavaScript

I have a unique URL structure that looks like this: index.html#page:page.php As I am loading my pages dynamically using AJAX, I want to create hotlinks to load specific pages. Currently, I am using the following function: function getdata(file){ $ ...

Exploring the use of Generics in Swift 5 for parsing JSON - comparing parsing from local files to

These are the two functions I've created to handle JSON parsing in Swift. The first one works perfectly with any JSON data I provide. However, the second function is causing me some trouble. It's supposed to do the same thing as the first one, b ...

Querying for common elements in PostgreSQL JSON arrays

In my database, I have a table with a JSONB data column that is structured like this: data: { "categories": [ "Category A", "Category D" ], "something": "dsa", } My goal is to query rows that contain one or more strings within ...

Switch the execution of the script by clicking on the button

My website has a script that hides the navigation when scrolling down and shows it again when scrolling up slightly. However, I need to disable this functionality on mobile when the hamburger menu button is clicked. How can I achieve this? Below is the cod ...

Why does my window scroll change when making a $.ajax call?

Encountering a peculiar bug... Whenever my JS code enters the success function after a jQuery $.ajax call, the scroll position on my window suddenly jumps to the bottom of the page. This is problematic for me as I'm developing a mobile page and want t ...

Having trouble with my ajax request not functioning correctly

<body style="margin:0px; padding:0px;" > <form method="post" > <input type="text" id="city" name="city" placeholder="Enter city name"> <input type="submit" value="Search City" id="searchid"/> ...

Stop hyperlinks from automatically opening in a new tab or window

I'm having trouble with my website links opening in new tabs. Even after changing the attributes to _self, it still doesn't work. Can someone please review my code below and provide a solution? Feel free to ask for more clarification if needed. ...

Transmitting JSON data containing nested elements to the server

This marks the beginning of my journey into building a native iOS app, so your patience is greatly appreciated. I am seeking guidance on how to properly structure this JSON data within an NSDictionary (my assumption for the correct method) in order to inc ...

Using PHP to decode a JSON array (Troubleshooting issues with Ajax JSON POST to PHP)

I am new to PHP and I have a jQuery/JavaScript based application that involves finding nearby places and presenting the results in a sorted order. I am attempting to move the sorting function from the HTML page to server-side PHP. This process includes ...

Loading a page with a Chitika ad using Jquery can cause site malfunctions

I am experiencing an issue with my header.php file on my website. I included another PHP file for my ad code, but it seems to load the ad and then redirect me to a blank page. Can someone please review my code and let me know if there is an error? Thank yo ...

Is there a way to modify the color scheme of the hamburger icon

I'm experiencing difficulties changing the color of a hamburger icon. I've searched for the specific code responsible for the color change, but I haven't been able to locate it. Ideally, I want the color to be white, but on small screens, i ...

Revolutionary Notification System - Harnessing the Power of Cutting-Edge notification

My system is working well, but I'm facing an issue with the GET method. It keeps using the same notification_id from the while loop for every search, resulting in repetitive requests. Here's an example of what's happening: jquery....486299 ...

Loading GLTF model via XHR may take an infinite amount of time to reach full completion

I am attempting to load a GLTF model of a piano using XHR and showcase the loading progress on a webpage. The model is being loaded utilizing the Three.js library. On a local server, everything works perfectly - the loading percentage is shown accurately, ...