unable to read a data from a external JSON file : Ionic
I have configured all the URL's in a JSON file called ionic-config.JSON
. The data is like
[
"db_url": "http://localhost:3000/users/"
]
I'm the trying to read the data using the GET
method.
url: string = '../ionic-config/ionic-config.json';
this.http.get(this.url).subscribe( (data: any) =>
this.db_url = data;
)
But I'm getting an error as 404 no found
GET http://localhost:8100/ionic-config/ionic-config.json 404 (Not
Found)
The file is in the directory src/ionic-config/ionic-config.json
angular ionic-framework ionic2 ionic3
add a comment |
I have configured all the URL's in a JSON file called ionic-config.JSON
. The data is like
[
"db_url": "http://localhost:3000/users/"
]
I'm the trying to read the data using the GET
method.
url: string = '../ionic-config/ionic-config.json';
this.http.get(this.url).subscribe( (data: any) =>
this.db_url = data;
)
But I'm getting an error as 404 no found
GET http://localhost:8100/ionic-config/ionic-config.json 404 (Not
Found)
The file is in the directory src/ionic-config/ionic-config.json
angular ionic-framework ionic2 ionic3
add a comment |
I have configured all the URL's in a JSON file called ionic-config.JSON
. The data is like
[
"db_url": "http://localhost:3000/users/"
]
I'm the trying to read the data using the GET
method.
url: string = '../ionic-config/ionic-config.json';
this.http.get(this.url).subscribe( (data: any) =>
this.db_url = data;
)
But I'm getting an error as 404 no found
GET http://localhost:8100/ionic-config/ionic-config.json 404 (Not
Found)
The file is in the directory src/ionic-config/ionic-config.json
angular ionic-framework ionic2 ionic3
I have configured all the URL's in a JSON file called ionic-config.JSON
. The data is like
[
"db_url": "http://localhost:3000/users/"
]
I'm the trying to read the data using the GET
method.
url: string = '../ionic-config/ionic-config.json';
this.http.get(this.url).subscribe( (data: any) =>
this.db_url = data;
)
But I'm getting an error as 404 no found
GET http://localhost:8100/ionic-config/ionic-config.json 404 (Not
Found)
The file is in the directory src/ionic-config/ionic-config.json
angular ionic-framework ionic2 ionic3
angular ionic-framework ionic2 ionic3
edited Nov 13 '18 at 17:33
georgeawg
33k104968
33k104968
asked Nov 13 '18 at 17:28
raj_txraj_tx
297
297
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Looks like an issue with your path.
Try this:
url: string = 'ionic-config/ionic-config.json';
Also, your logic for assigning the value of db_url is wrong. You are assigning the whole array rather than the field of a specific object in the array. Use this instead:
this.db_url = data[0].db_url;
I've tried it there is no change in the error. I'm calling it from the service file. The directory is assrc/services/AU.ts
.
– raj_tx
Nov 13 '18 at 18:23
add a comment |
You dont need to make a http request to the same server. To use a JSON file within your App:
For example if your JSON File is in /src/pages/YourPage/myJson.json
the go to your YourPage.ts
(in the same folder as your JSON) and say import * as jsonData from './myJson.json';
Now you can use it as normal JSON object without to parse it.
For example: (because your JSON is an Array of Objects):
console.log(jsonData[0].db_url);
I've tried it using the keywordimport
asimport * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error asCannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.
– raj_tx
Nov 14 '18 at 13:41
I see your error. Tryimport * as dbUrl from '../../assets/ionic-config.json'
– Jonathan
Nov 14 '18 at 14:02
Your link referred to the wrong folder, but now it should work
– Jonathan
Nov 14 '18 at 14:02
No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.
– raj_tx
Nov 14 '18 at 15:03
add a comment |
You can choose any of the following ways.
1. Using an Api Call
a. Just move the ionic-config.json
file to assets folder for better code flow.Then make the api call like this.
return this.http.get('assets/ionic-config.json')
.map(res=> return res.json);
b. in your .ts file
subscribe for the api call like this
<method_name_in_service_file>.subscribe(resp=>
this.db_url = resp;
,error=>
console.log(error)
)
2. Directly import from the ionic-config file
like this
import * as dbUrl from '<path to the config file>'
you will get the url from dbUrl[0].db_url
I've tried it using the keywordimport
asimport * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error asCannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.
– raj_tx
Nov 14 '18 at 13:41
Just use assets/<filename>
– Anand Raj
Nov 15 '18 at 5:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286533%2funable-to-read-a-data-from-a-external-json-file-ionic%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Looks like an issue with your path.
Try this:
url: string = 'ionic-config/ionic-config.json';
Also, your logic for assigning the value of db_url is wrong. You are assigning the whole array rather than the field of a specific object in the array. Use this instead:
this.db_url = data[0].db_url;
I've tried it there is no change in the error. I'm calling it from the service file. The directory is assrc/services/AU.ts
.
– raj_tx
Nov 13 '18 at 18:23
add a comment |
Looks like an issue with your path.
Try this:
url: string = 'ionic-config/ionic-config.json';
Also, your logic for assigning the value of db_url is wrong. You are assigning the whole array rather than the field of a specific object in the array. Use this instead:
this.db_url = data[0].db_url;
I've tried it there is no change in the error. I'm calling it from the service file. The directory is assrc/services/AU.ts
.
– raj_tx
Nov 13 '18 at 18:23
add a comment |
Looks like an issue with your path.
Try this:
url: string = 'ionic-config/ionic-config.json';
Also, your logic for assigning the value of db_url is wrong. You are assigning the whole array rather than the field of a specific object in the array. Use this instead:
this.db_url = data[0].db_url;
Looks like an issue with your path.
Try this:
url: string = 'ionic-config/ionic-config.json';
Also, your logic for assigning the value of db_url is wrong. You are assigning the whole array rather than the field of a specific object in the array. Use this instead:
this.db_url = data[0].db_url;
answered Nov 13 '18 at 18:05
Charis The ProgrammerCharis The Programmer
304212
304212
I've tried it there is no change in the error. I'm calling it from the service file. The directory is assrc/services/AU.ts
.
– raj_tx
Nov 13 '18 at 18:23
add a comment |
I've tried it there is no change in the error. I'm calling it from the service file. The directory is assrc/services/AU.ts
.
– raj_tx
Nov 13 '18 at 18:23
I've tried it there is no change in the error. I'm calling it from the service file. The directory is as
src/services/AU.ts
.– raj_tx
Nov 13 '18 at 18:23
I've tried it there is no change in the error. I'm calling it from the service file. The directory is as
src/services/AU.ts
.– raj_tx
Nov 13 '18 at 18:23
add a comment |
You dont need to make a http request to the same server. To use a JSON file within your App:
For example if your JSON File is in /src/pages/YourPage/myJson.json
the go to your YourPage.ts
(in the same folder as your JSON) and say import * as jsonData from './myJson.json';
Now you can use it as normal JSON object without to parse it.
For example: (because your JSON is an Array of Objects):
console.log(jsonData[0].db_url);
I've tried it using the keywordimport
asimport * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error asCannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.
– raj_tx
Nov 14 '18 at 13:41
I see your error. Tryimport * as dbUrl from '../../assets/ionic-config.json'
– Jonathan
Nov 14 '18 at 14:02
Your link referred to the wrong folder, but now it should work
– Jonathan
Nov 14 '18 at 14:02
No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.
– raj_tx
Nov 14 '18 at 15:03
add a comment |
You dont need to make a http request to the same server. To use a JSON file within your App:
For example if your JSON File is in /src/pages/YourPage/myJson.json
the go to your YourPage.ts
(in the same folder as your JSON) and say import * as jsonData from './myJson.json';
Now you can use it as normal JSON object without to parse it.
For example: (because your JSON is an Array of Objects):
console.log(jsonData[0].db_url);
I've tried it using the keywordimport
asimport * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error asCannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.
– raj_tx
Nov 14 '18 at 13:41
I see your error. Tryimport * as dbUrl from '../../assets/ionic-config.json'
– Jonathan
Nov 14 '18 at 14:02
Your link referred to the wrong folder, but now it should work
– Jonathan
Nov 14 '18 at 14:02
No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.
– raj_tx
Nov 14 '18 at 15:03
add a comment |
You dont need to make a http request to the same server. To use a JSON file within your App:
For example if your JSON File is in /src/pages/YourPage/myJson.json
the go to your YourPage.ts
(in the same folder as your JSON) and say import * as jsonData from './myJson.json';
Now you can use it as normal JSON object without to parse it.
For example: (because your JSON is an Array of Objects):
console.log(jsonData[0].db_url);
You dont need to make a http request to the same server. To use a JSON file within your App:
For example if your JSON File is in /src/pages/YourPage/myJson.json
the go to your YourPage.ts
(in the same folder as your JSON) and say import * as jsonData from './myJson.json';
Now you can use it as normal JSON object without to parse it.
For example: (because your JSON is an Array of Objects):
console.log(jsonData[0].db_url);
answered Nov 14 '18 at 6:46
JonathanJonathan
113
113
I've tried it using the keywordimport
asimport * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error asCannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.
– raj_tx
Nov 14 '18 at 13:41
I see your error. Tryimport * as dbUrl from '../../assets/ionic-config.json'
– Jonathan
Nov 14 '18 at 14:02
Your link referred to the wrong folder, but now it should work
– Jonathan
Nov 14 '18 at 14:02
No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.
– raj_tx
Nov 14 '18 at 15:03
add a comment |
I've tried it using the keywordimport
asimport * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error asCannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.
– raj_tx
Nov 14 '18 at 13:41
I see your error. Tryimport * as dbUrl from '../../assets/ionic-config.json'
– Jonathan
Nov 14 '18 at 14:02
Your link referred to the wrong folder, but now it should work
– Jonathan
Nov 14 '18 at 14:02
No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.
– raj_tx
Nov 14 '18 at 15:03
I've tried it using the keyword
import
as import * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.– raj_tx
Nov 14 '18 at 13:41
I've tried it using the keyword
import
as import * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.– raj_tx
Nov 14 '18 at 13:41
I see your error. Try
import * as dbUrl from '../../assets/ionic-config.json'
– Jonathan
Nov 14 '18 at 14:02
I see your error. Try
import * as dbUrl from '../../assets/ionic-config.json'
– Jonathan
Nov 14 '18 at 14:02
Your link referred to the wrong folder, but now it should work
– Jonathan
Nov 14 '18 at 14:02
Your link referred to the wrong folder, but now it should work
– Jonathan
Nov 14 '18 at 14:02
No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.
– raj_tx
Nov 14 '18 at 15:03
No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.
– raj_tx
Nov 14 '18 at 15:03
add a comment |
You can choose any of the following ways.
1. Using an Api Call
a. Just move the ionic-config.json
file to assets folder for better code flow.Then make the api call like this.
return this.http.get('assets/ionic-config.json')
.map(res=> return res.json);
b. in your .ts file
subscribe for the api call like this
<method_name_in_service_file>.subscribe(resp=>
this.db_url = resp;
,error=>
console.log(error)
)
2. Directly import from the ionic-config file
like this
import * as dbUrl from '<path to the config file>'
you will get the url from dbUrl[0].db_url
I've tried it using the keywordimport
asimport * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error asCannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.
– raj_tx
Nov 14 '18 at 13:41
Just use assets/<filename>
– Anand Raj
Nov 15 '18 at 5:19
add a comment |
You can choose any of the following ways.
1. Using an Api Call
a. Just move the ionic-config.json
file to assets folder for better code flow.Then make the api call like this.
return this.http.get('assets/ionic-config.json')
.map(res=> return res.json);
b. in your .ts file
subscribe for the api call like this
<method_name_in_service_file>.subscribe(resp=>
this.db_url = resp;
,error=>
console.log(error)
)
2. Directly import from the ionic-config file
like this
import * as dbUrl from '<path to the config file>'
you will get the url from dbUrl[0].db_url
I've tried it using the keywordimport
asimport * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error asCannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.
– raj_tx
Nov 14 '18 at 13:41
Just use assets/<filename>
– Anand Raj
Nov 15 '18 at 5:19
add a comment |
You can choose any of the following ways.
1. Using an Api Call
a. Just move the ionic-config.json
file to assets folder for better code flow.Then make the api call like this.
return this.http.get('assets/ionic-config.json')
.map(res=> return res.json);
b. in your .ts file
subscribe for the api call like this
<method_name_in_service_file>.subscribe(resp=>
this.db_url = resp;
,error=>
console.log(error)
)
2. Directly import from the ionic-config file
like this
import * as dbUrl from '<path to the config file>'
you will get the url from dbUrl[0].db_url
You can choose any of the following ways.
1. Using an Api Call
a. Just move the ionic-config.json
file to assets folder for better code flow.Then make the api call like this.
return this.http.get('assets/ionic-config.json')
.map(res=> return res.json);
b. in your .ts file
subscribe for the api call like this
<method_name_in_service_file>.subscribe(resp=>
this.db_url = resp;
,error=>
console.log(error)
)
2. Directly import from the ionic-config file
like this
import * as dbUrl from '<path to the config file>'
you will get the url from dbUrl[0].db_url
answered Nov 14 '18 at 12:27
Anand RajAnand Raj
705529
705529
I've tried it using the keywordimport
asimport * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error asCannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.
– raj_tx
Nov 14 '18 at 13:41
Just use assets/<filename>
– Anand Raj
Nov 15 '18 at 5:19
add a comment |
I've tried it using the keywordimport
asimport * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error asCannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.
– raj_tx
Nov 14 '18 at 13:41
Just use assets/<filename>
– Anand Raj
Nov 15 '18 at 5:19
I've tried it using the keyword
import
as import * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.– raj_tx
Nov 14 '18 at 13:41
I've tried it using the keyword
import
as import * as dbUrl from '../assets/ionic-config.json';
But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
. I have pasted the json file in the assets folder.– raj_tx
Nov 14 '18 at 13:41
Just use assets/<filename>
– Anand Raj
Nov 15 '18 at 5:19
Just use assets/<filename>
– Anand Raj
Nov 15 '18 at 5:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286533%2funable-to-read-a-data-from-a-external-json-file-ionic%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown