Skip to content
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

Angularjs 1.1.5 doesn't work for me #16

Closed
ndemoreau opened this issue Jun 6, 2013 · 7 comments
Closed

Angularjs 1.1.5 doesn't work for me #16

ndemoreau opened this issue Jun 6, 2013 · 7 comments

Comments

@ndemoreau
Copy link

Just to let you know. I didn't have time to investigate but I think it's linked to angular-resource.

It loads but not my data.

@NickClark
Copy link
Contributor

I think your issue may be due to this commit: angular/angular.js#1004

As it says in the commit, just add this to your module:

myAppModule.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);

@hiravgandhi
Copy link
Owner

@ndemoreau - Let me know if @NickClark's solution fixes your issues.

@gwright
Copy link

gwright commented Jun 23, 2013

I just spent some time digging into Angular and Rails to really understand this issue as it relates to $resource in a Rails environment.

It turns out that Rails is a bit cagey about utilizing the Accepts header from an HTTP request. It has to have the X-Requested-With header or it has to look like a request from something other than a browser, which means it can't have a wildcard mime type */*. Earlier versions of Angular sent the X-Requested-With header so the wildcard mime type didn't matter but, without that header, Rails thinks it is talking to a browser since $resource sends an Accept header of application/json, text/plain, */*.

So an alternative to adding the X-Requested-With header is to be more specific about what you are requesting. In my case instead of changing all the HTTP requests generated by $http, I just changed the requests generated by my use of $resource:

  App.factory('Record', function($resource) {
    return $resource("/records/:id", {
      id: "@id"
    }, {
      update: {
        method: "PUT"
      },
      query: {
        isArray: true,
        method: "GET",
        headers: {
          "Accept": "application/json",
          "X-Requested-With": "XMLHttpRequest"
        }
      }
    });
  });

@eddiemonge
Copy link

@gwright wont that only work for query requests so all your other requests will fail?

@gwright
Copy link

gwright commented Aug 23, 2013

@eddiemonge I haven't looked at this in a while but generally the other types of requests (PUT, POST, DELETE) have an empty body and so the format of an empty response is somewhat irrelevant. In those cases it is the status code that is of interest.

So perhaps it would be more correct send an explicit accepts header on all types of HTTP requests. Sending X-Requested-With is still problematic with respect to CORS process though, as I understand it.

@NickClark
Copy link
Contributor

Sounds like several solutions have been provided. Also this isn't an issue with angularjs-rails, so can this be closed? 👍

@hiravgandhi
Copy link
Owner

Closing this issues.

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

No branches or pull requests

5 participants