Skip to content

$stateProvider requests template in templateURL with accept header as application/json which must be text/html #1287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
defyjoy opened this issue Aug 24, 2014 · 7 comments
Labels
Milestone

Comments

@defyjoy
Copy link

defyjoy commented Aug 24, 2014

when we are actually requesting for a template url . The accept header is always "application/json" instead of "text/html"

I am currently Using nancy for generating both template and api request based on the content request type on accept headers .

Something like this

private dynamic Index(dynamic parameters)
{
List Students = NancyContext.Set().ToList();
return Negotiate.WithView("Index").WithModel(Students);
}

which basically when requested with 'application/json' returns json . And when requested with "text/html" then it returns the template with model . But since its requesting the template with 'application/json' . Its only returning json for the api request and not the template . Any workaround for to change the accept header to text/html instead of application/json for template url ?

@defyjoy defyjoy changed the title $stateProvider requests template as header with application/json $stateProvider requests template with accept header as application/json which must be text/html Aug 25, 2014
@defyjoy defyjoy changed the title $stateProvider requests template with accept header as application/json which must be text/html $stateProvider requests template in templateURL with accept header as application/json which must be text/html Aug 25, 2014
@christopherthielen
Copy link
Contributor

At first glance, a workaround might be to use an http interceptor

@christopherthielen
Copy link
Contributor

This is the default $http behavior; ui-router is not setting the accept header.

default Accept: Accept: application/json, text/plain, * / * https://docs.angularjs.org/api/ng/service/$http

See https://github.com/angular-ui/ui-router/blob/master/src/templateFactory.js#L85

@defyjoy
Copy link
Author

defyjoy commented Aug 27, 2014

Yes I understand . by default $http behavior accept header is application/json . But isnt that supposed to be text/html specially for templates which are served to application ? I mean in implementation of ui-router ? Cause almost all the cases in templateurl its always that a template would be an html only . so is it the only way for workaround for templateurl to be implemented by accept header application/json ?

If yes . Can I just change this interceptor for one route state and not for all routes globally in interceptor by anyway ?

and If no is there by any chance templateurl accept header can be changed manually or by defaults in ui-router can implement that (apart from http interceptor) ?

@nateabele
Copy link
Contributor

This seems legit. Templates should probably be requested with text/html by default.

@defyjoy
Copy link
Author

defyjoy commented Aug 31, 2014

Is there any thought going about this item ? Cause the reason is valid in case we are depending on a seamless backend which actually works with both api request and template request only based on accept headers . So we can achieve with a lot less code on backend . Rather than writing two different request handlers one for template another for api request . Just cause we dont request template with text/html

@christopherthielen christopherthielen added this to the 0.3.0 milestone Aug 31, 2014
jimobrien added a commit to jimobrien/ui-router that referenced this issue Sep 6, 2014
Add "Accept: text/html" to headers of $http.get invocation

Closes issue angular-ui#1287
@nateabele
Copy link
Contributor

Fixed in #1341.

@christopherthielen christopherthielen modified the milestones: 0.2.12, 0.3.0 Sep 6, 2014
@hecht-software
Copy link

In case you landed here due to a google search about this behavior:

In the current version, $templateRequest is used for loading templates and the fix in #1341 has no effect anymore. You have to configure the headers yourself in $templateRequestProvider:

angular.module('MyApp', ['ui.router']).config(['$templateRequestProvider', function($templateRequestProvider) {
    $templateRequestProvider.httpOptions({
        headers: { Accept: 'text/html' }
    });
}]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants