i worked with Angular +9 and i've a problem when read json file from assets folder.
i am using the http module and the call works fine when the application is run locally, but when i run ng-build and publish the application on a server, the call to the json file doesn't work indicating the following message:
ERROR Http failure response for assets/resources/fakeData.json: 0 Unknown Error
My code:
What could be the problem? Why can't I access the assets folder directly?
Your problem is that you are trying to access your file system with the HttpClient. You can't do that. You could of course make an API Endpoint that returns your json file, but you can't access it directly like that with the HttpClient module.
Here what I would do instead:
import * as myJson from '<path to fakeData.json>';
...
getData(): any {
return myJson;
}
If you are just trying to mess around with HttpClient module and need some json to play with, you use JSON Placeholder API:
this.http.get('https://jsonplaceholder.typicode.com/todos/1');
According to the below SOF Question, it looks like you can get your local json files to be hosted along with the angular dev server if you wanted to:
Load json from local file with http.get() in angular 2
I don't know which web server you are using, but it looks like your web server does not support hosting of JSON-files.
Apache: use the AddType directive - https://cets.seas.upenn.edu/answers/addtype.html
IIS: Add a MIME-type - https://learn.microsoft.com/en-us/iis/configuration/system.webserver/staticcontent/mimemap
Related
I have a url similar to https://www.nonexistentsite.com/fubar.json where fubar.json is a public json file which will download to your file system if you navigate to the url with your browser. I have a React web app where I want to read that file directly so as to display some of its data. I don't want to bother with any kind of a backend that would download the file to the apps file system so that it can read it. I want the React front end to read it directly in it's client side code with a fetch or an axios call or something like that. I'm familiar with the typical situation where I have a REST url like https://www.nonexistentsite.com/fubar which I can call and get the data. I'm failing to find info on how to handle this situation.
You could use Axios to load the data from the json file.
Example usage;
axios.get('https://www.nonexistentsite.com/fubar.json')
.then(jsoncontent => {
console.log(jsoncontent);
//do stuff with jsoncontent here
});
Maybe I'm misunderstanding your question, but I believe if you are just needing to fetch the json from a hosted file, you should be able to do so with axios.get(url)
I have an angular 2 project, how can I read and write to a JSON file on my server?
I can do what I want within my code itself bit I don't want to have to change my code, recompile and upload my website every time.
Any help? Examples are greatly appreciated
Angular can read the remote JSON file using the HTTP Client but it can't directly write to the remote file.
For writing, you can use a server side script such as PHP (supported by x10Hosting) to provide a url that allows Angular to post to (also using the HTTP Client), to update the JSON.
For example something like this PHP:
$data = json_decode('./data.json'); // decode the json
$data->something = $_POST['something']; // update the something property
file_put_contents('./data.json', json_encode($data)); // write back to data.json
I have an API who is generarted using loopback / strongloop and its running as follows.
and i want to export the the generated api into yaml or json so that i want to reuse it in another application. i m looking for swagger.json file.
SO in sawgger you get your json from your running api by going to localhost:3300/api-docs. How do i get it from here ?
You can do that with
localhost:3300/explorer/resources to get a list of all resources and localhost:3300/explorer/resource/ModelPluralName to get swagger for specific resource.
Also you can click on "Raw" link in your API explorer.
*Assuming of course that your application is running on localhost port 3300.
you can do that simply by typing
localhost:3300/explorer/swagger.json ,
you need to download the json file and load that into editor.swagger.io using file import then download it as YAML to feed into your restAPi ppliaction.
I have created a Liferay Web Service and made it accessible via JSON.
I call successfully call and use the JSON WebService using this URL:
https://localhost:8080/api/jsonws/my-portlet-name/my-method/time-unit/HOUR/class-name/TEST
I have annotated my Web Services like this:
#com.liferay.portal.security.ac.AccessControlled(guestAccessEnabled = true, hostAllowedValidationEnabled = false)
When I am using this call locally (http) I have no problems.
As soon as I deploy it to our test server (https) I do get "Forbidden - no permissions to access this resource".
What can I do to access my JSON Web Service calls ?
Ok, honestly : I donĀ“t know if there is an answer for that ;)
I have used the Liferay.Service js call to do the same and it works.
So no more URL calling from jQuery ...
I have a Meteor application which I am to get to make a JSON API request from another service on the server.
I tried using the JQuery package and $.getJSON but as the JQuery package doesn't have any effect on the server, I quickly found this doesn't work.
How can I get the server to make a JSON request?
As you say... it isn't available on the server. You could experiment with changing the jQuery package in the meteor folder though.
In /packages/jquery/package.js change:
Package.on_use(function (api) {
api.add_files('jquery.js', 'client');
});
To:
Package.on_use(function (api) {
api.add_files('jquery.js', ['client', 'server']);
});
and see if it works on the server side. You have to call the server with your local changed copy of meteor, for me I do this by using ../meteor/meteor, your path may vary.