Utilizing Ajax to retrieve data from Google Places API in JSON format

I've been working on a simple webpage that allows users to enter the name of a city in a text box and then displays basic information about it, such as the address and name. My main issue right now is with the Google Places API. Below is what I have managed to put together so far:

restaurants.html

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Restaurant Search</title>
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="jquery.js"></script>
        <script src="functions.js"></script>
        <script>
            $(document).ready(function(){
                getRestaurants();
            });
        </script>
    </head>
    <body>
        <div class="main">
            <form>
                Enter city name: <input id="city" type="text" name="cityname"><input id="btn" type="submit" value="Search">
            </form>
            <div id="error">
                The search field cannot be left empty!
            </div>
        </div>

        <div id="header">

        </div>
        <div id="items">

        </div>
    </body>
</html>

functions.js

function getRestaurants(){
    $("#btn").click(function(){
        if ($("#city").val().length==0){
            $("#error").css("display","block");
        }else{
            $("#error").css("display","none");
            var cityname = $("#city").val();
            var search_string = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+in+"+cityname+"&sensor=false&key=KEYKEYKEY";
            $.ajax({
                url: search_string
            }).done( function ( data ) {
                res ="";
                var obj = jQuery.parseJSON(data);
                $(obj).each(function(){
                    res+=this.name;
                });
                $("#items").text(res);
            });
        }
    });
}

style.css contains only this:

#error{
    display: none;
}

Can you spot the problem?

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

Switch the index of a JSON object within Fragments

I need to update the JSON object index within my QueryUtils.java file as I switch between different Fragments. Specifically, I want to modify this line: JSONObject currentDay = dayArray.getJSONObject(0); I have a total of 7 fragments and I am trying to ...

Using Three.js and WebGL to create transparent layers that conceal the planes positioned behind them

Have you ever noticed that in Three.js / WebGL, when you have two planes and one or both are transparent, the plane behind can be hidden by the transparent plane on top? Why does this happen? ...

There is an array present with data filled in, but unfortunately, I am unable to retrieve specific elements

As I work on my WordPress 4.7.2 website, I find myself making an AJAX call. The PHP function handling this call returns an array using wp_json_encode(). When I view the data array in the success callback of the AJAX function, everything looks just as expec ...

JavaScript Intercept Paste function allows you to detect and capture data being past

I stumbled upon this code snippet while browsing through how to intercept paste event in JavaScript. The code works well for intercepting clipboard data before it's pasted, ensuring that essential "\n" characters are not lost during the process. ...

unable to locate the PHP file on the server

Struggling to make an API call via POST method to retrieve data from a PHP file. Surprisingly, the code functions properly on another device without any hiccups. However, every time I attempt to initiate the call, I encounter this inconvenient error messag ...

Code snippet in Java to divide a JSON string into separate lines based on specific symbols

In my app, I need to generate a JSON text file with a specific structure: { "key1" : "value1" , "key2" : "value2" , "arrayKey" : [ { "keyA" : "valueA" , "keyB" : "valueB" , "keyC" : [ 0 , 1 , 2 ] } ] } This data needs to be formatted from JSONObject.toS ...

Combining data from various lists to populate a dictionary and generate a JSON file

I am currently working with a .dat file that I need to convert to json format. The challenge is that the data in the first half of the file has an awkward format that I must handle myself, so I cannot rely on pre-existing source code. After restructuring ...

Issue with resolving symbol JSON in Angular 7 when using JSON.stringify

Let me start off by saying that I am new to Angular 7. Currently, I am in the process of developing an application using Angular 7 with a C# backend. The specific challenge I am facing is the need to serialize an object in my component/service before sendi ...

Creating a WordPress template_redirect function that can handle two separate single.php files, allowing for both AJAX and non-AJ

I am currently working on a Wordpress blog with unique functionality. Each post thumbnail is contained within a small box, and when you click on one of them, the post expands and loads using ajax. I want this feature to load only the content of the post wi ...

Creating MySQL query results in Node.js manufacturing process

I am looking to format the MySQL result in Node.js in a specific way. The desired result format should look like this: [ { "m_idx" :1 , "contents" : { "m_name" : "a", ...

Tips on implementing multiple consecutive setState calls in ReactJS

Recently, I discovered that setState is an async call. I'm facing an issue where I need to utilize the updated result from setState immediately after setting it, but due to its async nature, I end up getting the old state value. While researching for ...

Turning $.post into $.ajax: A step-by-step guide

I need help with converting my $.post code to $.ajax. Here is the code snippet: $.post("../admin-login", { dataName:JSON.stringify({ username:uname, password:pass, }) }, function(data,status){ console.log("Data:"+data); answer = data; ...

Perform simple arithmetic operations between a number and a string using Angular within an HTML context

I'm stuck trying to find a straightforward solution to this problem. The array of objects I have contains two values: team.seed: number, team.placement: string In the team.placement, there are simple strings like 7 to indicate 7th place or something ...

An uncomplicated application designed to showcase a moving sphere

<!DOCTYPE html> <html> <head> <script type="text/javascript"> var canvas_element; var initial_x = 200; var initial_y = 300; var x_movement = 1; function start_canvas() {setInterval(draw_circle, 10); canvas_element = bouncing_ball_c ...

Using an asynchronous pipe filter with the ngFor loop in Angular 2 for efficient data

I have a JSON array that I need to iterate through in order to display data using an NGfor Loop. My goal is to apply filters after the data has been loaded to refine the results. The issue I am facing is that my pipe filter is returning 'cannot read p ...

How can I close the menu when a menu item is clicked in a React.js application?

I am facing an issue where I want to close the menu when clicking on any menu item in React JS. The functionality works fine with a button icon but not with individual menu items. How can I resolve this problem? I have been trying to implement it using CSS ...

What is the best way to compare two arrays in my JSON data?

Hello, I'm trying to compare two arrays - one from my JSON data and the second from a regular array. Specifically, I want to check if the ids of "cm:taggable" exist in my secondArray. JSON { "entry": { "isFile": true, "createdByUs ...

The Ajax Auto Complete function encounters an issue when attempting to assign a value to the TextBox

I have a Textbox that utilizes an Ajax Autocomplete function. The function is functioning correctly and retrieves values from the database, populating them in the TextBox as well. However, there is a button on the page that triggers a query, fetching som ...

An Android error message is displaying: "JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject"

My introduction to JSON is quite recent, and I've been watching Youtube videos for guidance on handling the error described in this link: question. Despite my efforts, I'm still confused as to why I keep encountering the same error. Here's ...

Unable to adjust price slider on mobile device, bars are not sliding

I have been utilizing the jQuery slider from lugolabs.com for my website, and while it works flawlessly on desktop, it seems to have issues when viewed on mobile devices. You can check out the slider at this link: The library used for this slider is jQue ...