ways to assign local file path as image url in json

Hey there, I am a new tool for angularjs.

Take a look at the json file below:

[
  {
   "name": "WORLD",
   "population": 6916183000,
   "flagurl":"C:\xampp\htdocs\selva
\Flag_of_the_People's_Republic_of_China.svg"
  },
  {
   "name":"More developed regions",
   "population": 1240935000,
   "flagurl": "C:\xampp\htdocs\selva
\Flag_of_the_People's_Republic_of_China.svg"
  },
  {
    "name": "Less developed regions",
    "population": 5675249000,
    "flagurl": "C:\xampp\htdocs\selva
\Flag_of_the_People's_Republic_of_China.svg"
  },
  {
    "name": "Least developed countries",
    "population": 838807000,
    "flagurl": "C:\xampp\htdocs\selva
\Flag_of_the_People's_Republic_of_China.svg"
  }
]

Unfortunately, when I try to run the code below, it doesn't seem to be working properly,

<script>
var myapp=angular.module('myapp',[]);
myapp.controller('myctrl', function ($scope,$http) {
$http.get('flag.json').success(function(data){
$scope.countries=data;
});
});
</script>
<body>
<div ng-controller="myctrl">
search: <input type="text" ng-model="query" />
<table>
<tr>
<th>country</th>
<th>population</th>
<th>flag</th>
</tr>
<tr ng-repeat="country in countries"> 
<td>{{country.name}}</td>
<td>{{country.population}}</td>
<td><img src="{{country.flagurl}}" width="100"></td>
</tr>
</table>

Interestingly, the code works fine when fetching images online.

I'm trying to figure out what mistake I have made in my coding. Any help or tips would be greatly appreciated!

Thank you,

Answer №1

The issue arises from the failure to load the images due to incorrect references. It is essential to reference the images based on their relative location to the index.html file. Since the images are located in the same directory as the index.html, there is no need for additional file paths.

To resolve the image loading problem, consider restructuring the JSON as follows:

{
  "flagurl": "Flag_of_the_People's_Republic_of_China.svg"
}

Answer №2

Modern browsers now block access to local resources by default (not certain about Internet Explorer), but you can bypass this restriction in Chrome by launching it with the command "chrome.exe --allow-file-access-from-files". This will grant you access to local files.

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

Combining Multiple 3D JSON Objects in JavaScript [or jQuery]

Looking to combine multiple JSON objects into one? I have a code snippet that you can use! Here is the code: http://jsfiddle.net/5Uz27/ The issue with this code is that it only merges objects on the first level, causing deeper levels to be overwritten. T ...

AngularJS smart-table / Single row selection

I'm trying to create selectable table rows in smart-table. Using st-select-row="row" and st-select-mode="single" However, I want a selected row to remain selected until the next row is clicked. Currently, when I click on links that open in a new w ...

What methods can be used to seamlessly exchange data between a Rails application and the client on a consistent basis?

I'm interested in creating a ruby method that will return a Hash of data, and I want it to be included in the application.js file upon app initialization. specifics: all definitions are currently located here (in lib or config directories, perhaps?) ...

Merge the elements of one array with the elements of another array simultaneously

Can a function be created that combines each value from two arrays, even if the arrays are of different lengths? For example: arr1 = ["apple", "banana", "cherry"]; arr2 = ["pear", "kiwi", "orange"] mergedArr= ["applepear", "applekiwi", "appleorange", " ...

Retrieving information from an API using JSON and then combining two separate objects into one row within an Array Adapter

Here is some code that fetches data from an API and displays it in a List View. The goal is to show both the "rate" and the corresponding "name" on the same row, making it more user-friendly for the users. The AsyncTask provided below successfully retriev ...

Stop the form from submitting when the enter key is pressed using VueJS and Semantic UI

After spending the past two days searching for a solution to this persistent issue, none of the suggested remedies have proven effective so far. My form's HTML structure is as follows: <form id="quote_form" action="" method="get" class="ui large ...

Sending a string data from client to a MongoDB database using Express.js and Node.js with the help of Sails framework

Need help with sending a string from client to mongoDB using Sails The "val" variable represents a string sent in real time from the client to the server database POST Form( is using HTML the best approach here? ) <!DOCTYPE html> <html> < ...

Combining multiple 'Eithers' and 'Promises' in fp-ts: A guide to piping and chaining operations

Recently, I began working with fp-ts and wanted to create a method with functional-like behavior that would: Parse a bearer token Verify the validity of the user using the parsed token import { Request } from 'express'; import { either } from & ...

Ensuring that the constraints values in JSON Schema Validation are equal to each other

Can we ensure that the value of one field matches another field when validating a payload schema? If yes, how can this be accomplished? For example, in the following payload, the email field should match the login field: Payload : { "email":"<a h ...

Swift Data in JSON Format

After receiving the JSON data displayed below, my goal is to extract the "player_name" & "player_country" information. How can I achieve this?**enter image description here If the image fails to load, refer to the output provided: ["team_key": 2 ...

Trouble with minified scripts on a particular server

Apologies if this has already been addressed, but I've been searching for a solution for hours. I created a basic website that works perfectly on my own hosting. However, when I transfer it to new hosting (same company, same package, different server ...

Automatically focus on new page when button is clicked (in a separate HTML file)

I successfully added auto-focus functionality to a button that opens a modal using the code below: Created with AngularJS: app.directive('autoFocus', function($timeout) { return { restrict: 'AC', link: function(_sc ...

jQuery AJAX Concurrent Event

I have encountered a problem similar to ones discussed on SO, but none exactly match my specific issue. Here's the situation: var x = 5; if( some condition ){ $.ajax({ url:"...", success: function(response){ x = respo ...

Ensure a button is automatically highlighted within an active class script

I have created a set of buttons that allow users to change the font family of the text. I would like the border and text color to update automatically when a specific option is selected. Currently, the JavaScript code works well, but when the page first l ...

Looking to maintain the value of a toggle button in a specific state depending on certain condition checks

I have a situation where I need to keep a toggle button set to "off" if my collection object is empty. Previously, I was using v-model to update the value of the toggle button. However, now I am attempting to use :value and input events, but I am strugglin ...

Discovering strings within strings on the Android platform

In my current project, I am dealing with a lengthy JSON text that contains movie titles. I need to extract these titles from the following pattern: ((((((( .","title":" AND ","type":" ))))))) After researching other solutions, I came across one appro ...

Rearranging information within a JSON structure

Here is a sample of Javascript object and code to consider: Javascript Object { "thing":{ "data":"some data", "thumb":"some data", "data1":"some data", "data2":"some data", "data3":"some data", }, "extra1":[ ...

Executing the outer function from within the inner function of a different outer function

Imagine this scenario: function firstFunction() { console.log("This is the first function") } secondFunction() { thirdFunction() { //call firstFunction inside thirdFunction } } What is the way to invoke firstFunction from thirdFunction? ...

Analyzing JSON information and presenting findings within a table

Requesting user input to generate a JSON object based on their response. Interested in showcasing specific details from the object in a table format for user viewing. Questioning the efficiency and clarity of current approach. My current progress: In HTM ...

Encountering a 404 error when attempting to post from a Node.js express app to a

Trying to post to a MySQL database has been giving me a 404 error. I have searched through various posts here, but none of the accepted solutions seem to work for me. I'm struggling to figure out what I am doing wrong. When utilizing a GET request, t ...