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
request.xhr? is not working in controller #16444
Comments
It is not deprecated. What is the request you are making?
|
@rafaelfranca I just want to tell if the request is a ajax request. And I googled the answer that request.xhr? is the very method I need. But in debugging, the calling of request.xhr? always returns nil. |
@Edward1992 are you testing with ajax requests or just with non-ajax requests? |
Maybe because the request is always a non-Ajax request. How can I reproduce this behavior?
|
@rafaelfranca @guilleiguaran |
Check in web inspector to see whether you're getting the Cross-origin jQuery requests drop it. If you do XHR by hand, you need to On Sat, Aug 9, 2014 at 12:26 PM, Edward notifications@github.com wrote:
|
Actually I'm integrating angularJS with rails. The ajax request is sent by Angular through its $http. I wonder if it is the csrf-token that caused the problem. And I wrote some codes that goes like:
But in the server runtime, when I puts binding.pry to the action which is responding the ajax request, the calling of request.xhr? is still returning nil. Neither true nor false. |
About the return value you can read #5329 and all related issues. |
Angular 1.1.1 removed the X-Requested-With header to avoid triggering CORS preflights. Add it back yourself to fix. See the Angular changelog for a full rundown: https://github.com/angular/angular.js/blob/master/CHANGELOG.md#breaking-changes-28 |
@rafaelfranca @jeremy Thanks for the tips:) |
my app come to this problem too. it's just a simple remote link ( not handle by myself !), but when i can check the request type use request.xhr? , it just return zero. request.xhr? return nil when request is a normal request, why? Ajax request was attached with the correct header option which is "X-Requested-With:XMLHttpRequest". rails version 4.1.6 |
If you look at the code for the xhr? method, it's doing a regex match on the request @env variable. It's returning a 0 for true because of this: The regex match starts with the very first letter, so you get a 0. If it didn't match, you would get nil. Hope that makes sense. |
@dza2000 Good call on that! I added to our project a method to make it more clear: def js?
xhr? == 0
end |
@eyaleizenberg that is a bad idea, and I must discourage it: in a future version of Rails, |
Another approach def xhr_or_js?
request.xhr? || request.format.js?
end |
For completeness, to add back the header as default for every request, this is required: myAppModule.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]); For 1.1.x+ myapp.factory('httpRequestInterceptor', function () {
return {
request: function (config) {
config.headers["X-Requested-With"] = 'XMLHttpRequest';
return config;
}
};
});
myapp.config(function ($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
}); |
I wrote this action and used request.xhr? method to tell if the request is sent via ajax. But what I found in debugging is that the result returned by xhr? is always nil. A ruby method whose name ends with question mark is supposed to always return true or false, isn't it?
I wonder if request.xhr? is deprecated in Rails 4.1.
The text was updated successfully, but these errors were encountered: