Angular http.get error with responseType: 'textAngular no provider for NameServiceAngular HTML binding@Directive v/s @Component in AngularHow can I get new selection in “select” in Angular 2?How to pass url arguments (query string) to a HTTP request on Angular?Angular HTTP GET with TypeScript error http.get(…).map is not a function in [null]Angular2 http.get() ,map(), subscribe() and observable pattern - basic understandingAngular 2 beta.17: Property 'map' does not exist on type 'Observable<Response>'Angular/RxJs When should I unsubscribe from `Subscription`Huge number of files generated for every Angular project
What is a CirKle Word™?
Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?
How to prevent clipped screen edges on my TV, HDMI-connected?
Irish Snap: Variant Rules
How do you harvest carrots in creative mode?
Which note goes on which side of the stem?
Is there any method of inflicting the incapacitated condition and no other condition?
Why can't I choose blocks attached with pulley B as a system?
Are modern clipless shoes and pedals that much better than toe clips and straps?
Best clipless pedals for sore feet?
Defense against attacks using dictionaries
Would this system work to purify water?
Is “I am getting married with my sister” ambiguous?
Why do banks “park” their money at the European Central Bank?
Is there any practical application for performing a double Fourier transform? ...or an inverse Fourier transform on a time-domain input?
In French culture, does the metaphorical phrase "lui jeter des assiettes en pleine figure" convey the idea of "having a heated argument"?
Do they have Supervillain(s)?
What is the history of the university asylum law?
What is this symbol: semicircles facing each other?
Are there any elected officials in the U.S. who are not legislators, judges, or constitutional officers?
How to find multiple values on the same line in any permutation using Notepad++?
Compelling story with the world as a villain
Can you feel passing through the sound barrier in an F-16?
How to find out the average duration of the peer-review process for a given journal?
Angular http.get error with responseType: 'text
Angular no provider for NameServiceAngular HTML binding@Directive v/s @Component in AngularHow can I get new selection in “select” in Angular 2?How to pass url arguments (query string) to a HTTP request on Angular?Angular HTTP GET with TypeScript error http.get(…).map is not a function in [null]Angular2 http.get() ,map(), subscribe() and observable pattern - basic understandingAngular 2 beta.17: Property 'map' does not exist on type 'Observable<Response>'Angular/RxJs When should I unsubscribe from `Subscription`Huge number of files generated for every Angular project
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Whenever i try to use requestType: 'text' in my http.get(url) call I get an error that I am unable to diff and only arrays and iterables are allowed; however, I am taking my object and transforming it into an array. I need some assistance understanding my error and how to fix my situation
When I remove the RequestType the array comes through no issues and displays on my front end.
----service-----
getAll()
const requestOptions: Object =
/* other options here */
responseType: 'text'
return this.http.get<any>(this.url, requestOptions);
---component .ts-----
notificationsFass: any[];
constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService)
this.notificationsFass = [];
ngOnInit()
this.notificationService.getAll()
.subscribe(notificationsFass =>
this.notificationsFass = notificationsFass;
);
---html---
<div *ngFor="let m of notificationsFass">
---error----
ERROR Error: Error trying to diff '["incidentNumber":700,"createdByName":"FASS Notification","createdDate":"2019-03-27T09:18:15.000+0000"]'. Only arrays and iterables are allowed
|
show 1 more comment
Whenever i try to use requestType: 'text' in my http.get(url) call I get an error that I am unable to diff and only arrays and iterables are allowed; however, I am taking my object and transforming it into an array. I need some assistance understanding my error and how to fix my situation
When I remove the RequestType the array comes through no issues and displays on my front end.
----service-----
getAll()
const requestOptions: Object =
/* other options here */
responseType: 'text'
return this.http.get<any>(this.url, requestOptions);
---component .ts-----
notificationsFass: any[];
constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService)
this.notificationsFass = [];
ngOnInit()
this.notificationService.getAll()
.subscribe(notificationsFass =>
this.notificationsFass = notificationsFass;
);
---html---
<div *ngFor="let m of notificationsFass">
---error----
ERROR Error: Error trying to diff '["incidentNumber":700,"createdByName":"FASS Notification","createdDate":"2019-03-27T09:18:15.000+0000"]'. Only arrays and iterables are allowed
1
What is the actual content-type being sent from the server? If the content-type isapplication/jsonthere would be no reason to useresponseType: 'text'. The "data" displayed in the error looks to be JSON, so you can just use the defaultresponseTypeso that HttpClient parses the received JSON for use in your template. What is the reason you are wanting to useresponseType: 'text'?
– Alexander Staroselsky
Mar 27 at 17:27
1
however, I am taking my object and transforming it into an array.: where? The posted code doesn't do that anywhere. You're trying to iterate through a string.
– JB Nizet
Mar 27 at 17:29
It is application/json ; however, I am getting httpResponse errors from my server when it can't render JSON from what I've read that if I can have the httpResponse text I may get rid of those errors
– Rob DePietro
Mar 27 at 17:31
2
No, you are taking your text response from the server (a string) and overwriting the empty array that isnotificationFass.
– UncleDave
Mar 27 at 17:34
1
Right,*ngForcan only render objects of a specific format such as an array of objects. Definitely do not useresponseType: 'text'if your content-type isapplication/json. It will not be parsed and will simply be a string that under no circumstances could be used by structural directives such as*ngFor. You should probably share the logic you had taking the response prior to thetextresponseTypechange and turning it into an array.
– Alexander Staroselsky
Mar 27 at 17:34
|
show 1 more comment
Whenever i try to use requestType: 'text' in my http.get(url) call I get an error that I am unable to diff and only arrays and iterables are allowed; however, I am taking my object and transforming it into an array. I need some assistance understanding my error and how to fix my situation
When I remove the RequestType the array comes through no issues and displays on my front end.
----service-----
getAll()
const requestOptions: Object =
/* other options here */
responseType: 'text'
return this.http.get<any>(this.url, requestOptions);
---component .ts-----
notificationsFass: any[];
constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService)
this.notificationsFass = [];
ngOnInit()
this.notificationService.getAll()
.subscribe(notificationsFass =>
this.notificationsFass = notificationsFass;
);
---html---
<div *ngFor="let m of notificationsFass">
---error----
ERROR Error: Error trying to diff '["incidentNumber":700,"createdByName":"FASS Notification","createdDate":"2019-03-27T09:18:15.000+0000"]'. Only arrays and iterables are allowed
Whenever i try to use requestType: 'text' in my http.get(url) call I get an error that I am unable to diff and only arrays and iterables are allowed; however, I am taking my object and transforming it into an array. I need some assistance understanding my error and how to fix my situation
When I remove the RequestType the array comes through no issues and displays on my front end.
----service-----
getAll()
const requestOptions: Object =
/* other options here */
responseType: 'text'
return this.http.get<any>(this.url, requestOptions);
---component .ts-----
notificationsFass: any[];
constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService)
this.notificationsFass = [];
ngOnInit()
this.notificationService.getAll()
.subscribe(notificationsFass =>
this.notificationsFass = notificationsFass;
);
---html---
<div *ngFor="let m of notificationsFass">
---error----
ERROR Error: Error trying to diff '["incidentNumber":700,"createdByName":"FASS Notification","createdDate":"2019-03-27T09:18:15.000+0000"]'. Only arrays and iterables are allowed
edited Mar 27 at 17:27
Rob DePietro
asked Mar 27 at 17:23
Rob DePietroRob DePietro
441 silver badge8 bronze badges
441 silver badge8 bronze badges
1
What is the actual content-type being sent from the server? If the content-type isapplication/jsonthere would be no reason to useresponseType: 'text'. The "data" displayed in the error looks to be JSON, so you can just use the defaultresponseTypeso that HttpClient parses the received JSON for use in your template. What is the reason you are wanting to useresponseType: 'text'?
– Alexander Staroselsky
Mar 27 at 17:27
1
however, I am taking my object and transforming it into an array.: where? The posted code doesn't do that anywhere. You're trying to iterate through a string.
– JB Nizet
Mar 27 at 17:29
It is application/json ; however, I am getting httpResponse errors from my server when it can't render JSON from what I've read that if I can have the httpResponse text I may get rid of those errors
– Rob DePietro
Mar 27 at 17:31
2
No, you are taking your text response from the server (a string) and overwriting the empty array that isnotificationFass.
– UncleDave
Mar 27 at 17:34
1
Right,*ngForcan only render objects of a specific format such as an array of objects. Definitely do not useresponseType: 'text'if your content-type isapplication/json. It will not be parsed and will simply be a string that under no circumstances could be used by structural directives such as*ngFor. You should probably share the logic you had taking the response prior to thetextresponseTypechange and turning it into an array.
– Alexander Staroselsky
Mar 27 at 17:34
|
show 1 more comment
1
What is the actual content-type being sent from the server? If the content-type isapplication/jsonthere would be no reason to useresponseType: 'text'. The "data" displayed in the error looks to be JSON, so you can just use the defaultresponseTypeso that HttpClient parses the received JSON for use in your template. What is the reason you are wanting to useresponseType: 'text'?
– Alexander Staroselsky
Mar 27 at 17:27
1
however, I am taking my object and transforming it into an array.: where? The posted code doesn't do that anywhere. You're trying to iterate through a string.
– JB Nizet
Mar 27 at 17:29
It is application/json ; however, I am getting httpResponse errors from my server when it can't render JSON from what I've read that if I can have the httpResponse text I may get rid of those errors
– Rob DePietro
Mar 27 at 17:31
2
No, you are taking your text response from the server (a string) and overwriting the empty array that isnotificationFass.
– UncleDave
Mar 27 at 17:34
1
Right,*ngForcan only render objects of a specific format such as an array of objects. Definitely do not useresponseType: 'text'if your content-type isapplication/json. It will not be parsed and will simply be a string that under no circumstances could be used by structural directives such as*ngFor. You should probably share the logic you had taking the response prior to thetextresponseTypechange and turning it into an array.
– Alexander Staroselsky
Mar 27 at 17:34
1
1
What is the actual content-type being sent from the server? If the content-type is
application/json there would be no reason to use responseType: 'text'. The "data" displayed in the error looks to be JSON, so you can just use the default responseType so that HttpClient parses the received JSON for use in your template. What is the reason you are wanting to use responseType: 'text'?– Alexander Staroselsky
Mar 27 at 17:27
What is the actual content-type being sent from the server? If the content-type is
application/json there would be no reason to use responseType: 'text'. The "data" displayed in the error looks to be JSON, so you can just use the default responseType so that HttpClient parses the received JSON for use in your template. What is the reason you are wanting to use responseType: 'text'?– Alexander Staroselsky
Mar 27 at 17:27
1
1
however, I am taking my object and transforming it into an array.: where? The posted code doesn't do that anywhere. You're trying to iterate through a string.
– JB Nizet
Mar 27 at 17:29
however, I am taking my object and transforming it into an array.: where? The posted code doesn't do that anywhere. You're trying to iterate through a string.
– JB Nizet
Mar 27 at 17:29
It is application/json ; however, I am getting httpResponse errors from my server when it can't render JSON from what I've read that if I can have the httpResponse text I may get rid of those errors
– Rob DePietro
Mar 27 at 17:31
It is application/json ; however, I am getting httpResponse errors from my server when it can't render JSON from what I've read that if I can have the httpResponse text I may get rid of those errors
– Rob DePietro
Mar 27 at 17:31
2
2
No, you are taking your text response from the server (a string) and overwriting the empty array that is
notificationFass.– UncleDave
Mar 27 at 17:34
No, you are taking your text response from the server (a string) and overwriting the empty array that is
notificationFass.– UncleDave
Mar 27 at 17:34
1
1
Right,
*ngFor can only render objects of a specific format such as an array of objects. Definitely do not use responseType: 'text' if your content-type is application/json. It will not be parsed and will simply be a string that under no circumstances could be used by structural directives such as *ngFor. You should probably share the logic you had taking the response prior to the text responseType change and turning it into an array.– Alexander Staroselsky
Mar 27 at 17:34
Right,
*ngFor can only render objects of a specific format such as an array of objects. Definitely do not use responseType: 'text' if your content-type is application/json. It will not be parsed and will simply be a string that under no circumstances could be used by structural directives such as *ngFor. You should probably share the logic you had taking the response prior to the text responseType change and turning it into an array.– Alexander Staroselsky
Mar 27 at 17:34
|
show 1 more comment
1 Answer
1
active
oldest
votes
Based on the json in the error message you need to do the following:
- Define an interface, I used the name
INotification. This will define the members available on the deserialized json response. - Strongly type the method return types and also supply the generic type argument in
http.get<T>. Whenhttp.getis called it will try to deserialize the json response from the server to an object graph. By definingINotification[]as the returned type further callers, like from a component, can now safely call members on the return type likefindor other Array.prototype members as well as access the defined members on instances in the array.
responseType: 'text' is only necessary when you are not emitting a response from the server or when the response is text and not json. The former can happen with post or put or delete calls where the server might only send a status and no message body in the response.
Here is your service code rewritten based on the feedback above.
notificationsFassService.ts
export interface INotification
incidentNumber: number;
createdByName: string;
createdDate: string;
export class NotificationsFassService
constructor (private readonly http: HttpClient)
getAll():Observable<INotification[]>
return this.http.get<INotification[]>(this.url);
notificationsFassComponent.ts
export class NotificationsFassComponent implements OnInit
notificationsFass: INotification[];
constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService)
this.notificationsFass = [];
ngOnInit()
this.notificationService.getAll()
.subscribe(notificationsFass =>
this.notificationsFass = notificationsFass;
);
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%2f55383182%2fangular-http-get-error-with-responsetype-text%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Based on the json in the error message you need to do the following:
- Define an interface, I used the name
INotification. This will define the members available on the deserialized json response. - Strongly type the method return types and also supply the generic type argument in
http.get<T>. Whenhttp.getis called it will try to deserialize the json response from the server to an object graph. By definingINotification[]as the returned type further callers, like from a component, can now safely call members on the return type likefindor other Array.prototype members as well as access the defined members on instances in the array.
responseType: 'text' is only necessary when you are not emitting a response from the server or when the response is text and not json. The former can happen with post or put or delete calls where the server might only send a status and no message body in the response.
Here is your service code rewritten based on the feedback above.
notificationsFassService.ts
export interface INotification
incidentNumber: number;
createdByName: string;
createdDate: string;
export class NotificationsFassService
constructor (private readonly http: HttpClient)
getAll():Observable<INotification[]>
return this.http.get<INotification[]>(this.url);
notificationsFassComponent.ts
export class NotificationsFassComponent implements OnInit
notificationsFass: INotification[];
constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService)
this.notificationsFass = [];
ngOnInit()
this.notificationService.getAll()
.subscribe(notificationsFass =>
this.notificationsFass = notificationsFass;
);
add a comment |
Based on the json in the error message you need to do the following:
- Define an interface, I used the name
INotification. This will define the members available on the deserialized json response. - Strongly type the method return types and also supply the generic type argument in
http.get<T>. Whenhttp.getis called it will try to deserialize the json response from the server to an object graph. By definingINotification[]as the returned type further callers, like from a component, can now safely call members on the return type likefindor other Array.prototype members as well as access the defined members on instances in the array.
responseType: 'text' is only necessary when you are not emitting a response from the server or when the response is text and not json. The former can happen with post or put or delete calls where the server might only send a status and no message body in the response.
Here is your service code rewritten based on the feedback above.
notificationsFassService.ts
export interface INotification
incidentNumber: number;
createdByName: string;
createdDate: string;
export class NotificationsFassService
constructor (private readonly http: HttpClient)
getAll():Observable<INotification[]>
return this.http.get<INotification[]>(this.url);
notificationsFassComponent.ts
export class NotificationsFassComponent implements OnInit
notificationsFass: INotification[];
constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService)
this.notificationsFass = [];
ngOnInit()
this.notificationService.getAll()
.subscribe(notificationsFass =>
this.notificationsFass = notificationsFass;
);
add a comment |
Based on the json in the error message you need to do the following:
- Define an interface, I used the name
INotification. This will define the members available on the deserialized json response. - Strongly type the method return types and also supply the generic type argument in
http.get<T>. Whenhttp.getis called it will try to deserialize the json response from the server to an object graph. By definingINotification[]as the returned type further callers, like from a component, can now safely call members on the return type likefindor other Array.prototype members as well as access the defined members on instances in the array.
responseType: 'text' is only necessary when you are not emitting a response from the server or when the response is text and not json. The former can happen with post or put or delete calls where the server might only send a status and no message body in the response.
Here is your service code rewritten based on the feedback above.
notificationsFassService.ts
export interface INotification
incidentNumber: number;
createdByName: string;
createdDate: string;
export class NotificationsFassService
constructor (private readonly http: HttpClient)
getAll():Observable<INotification[]>
return this.http.get<INotification[]>(this.url);
notificationsFassComponent.ts
export class NotificationsFassComponent implements OnInit
notificationsFass: INotification[];
constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService)
this.notificationsFass = [];
ngOnInit()
this.notificationService.getAll()
.subscribe(notificationsFass =>
this.notificationsFass = notificationsFass;
);
Based on the json in the error message you need to do the following:
- Define an interface, I used the name
INotification. This will define the members available on the deserialized json response. - Strongly type the method return types and also supply the generic type argument in
http.get<T>. Whenhttp.getis called it will try to deserialize the json response from the server to an object graph. By definingINotification[]as the returned type further callers, like from a component, can now safely call members on the return type likefindor other Array.prototype members as well as access the defined members on instances in the array.
responseType: 'text' is only necessary when you are not emitting a response from the server or when the response is text and not json. The former can happen with post or put or delete calls where the server might only send a status and no message body in the response.
Here is your service code rewritten based on the feedback above.
notificationsFassService.ts
export interface INotification
incidentNumber: number;
createdByName: string;
createdDate: string;
export class NotificationsFassService
constructor (private readonly http: HttpClient)
getAll():Observable<INotification[]>
return this.http.get<INotification[]>(this.url);
notificationsFassComponent.ts
export class NotificationsFassComponent implements OnInit
notificationsFass: INotification[];
constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService)
this.notificationsFass = [];
ngOnInit()
this.notificationService.getAll()
.subscribe(notificationsFass =>
this.notificationsFass = notificationsFass;
);
answered Mar 27 at 17:47
IgorIgor
45.1k4 gold badges56 silver badges116 bronze badges
45.1k4 gold badges56 silver badges116 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55383182%2fangular-http-get-error-with-responsetype-text%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
1
What is the actual content-type being sent from the server? If the content-type is
application/jsonthere would be no reason to useresponseType: 'text'. The "data" displayed in the error looks to be JSON, so you can just use the defaultresponseTypeso that HttpClient parses the received JSON for use in your template. What is the reason you are wanting to useresponseType: 'text'?– Alexander Staroselsky
Mar 27 at 17:27
1
however, I am taking my object and transforming it into an array.: where? The posted code doesn't do that anywhere. You're trying to iterate through a string.
– JB Nizet
Mar 27 at 17:29
It is application/json ; however, I am getting httpResponse errors from my server when it can't render JSON from what I've read that if I can have the httpResponse text I may get rid of those errors
– Rob DePietro
Mar 27 at 17:31
2
No, you are taking your text response from the server (a string) and overwriting the empty array that is
notificationFass.– UncleDave
Mar 27 at 17:34
1
Right,
*ngForcan only render objects of a specific format such as an array of objects. Definitely do not useresponseType: 'text'if your content-type isapplication/json. It will not be parsed and will simply be a string that under no circumstances could be used by structural directives such as*ngFor. You should probably share the logic you had taking the response prior to thetextresponseTypechange and turning it into an array.– Alexander Staroselsky
Mar 27 at 17:34