301 redirect on AJAX -- redirected? - ajax

I have an AJAX call to a server endpoint that does a 301 redirect to the same page, but with a trailing slash.
Does the browser follow redirects when called with AJAX or does it ignore them? In my case it doesn't follow them, but I figured it might be something from the server config.

If you are using jquery you could look at the questions below to implement it. By default jQuery (and most libraries with Ajax) don't follow redirects by default:
How to manage a redirect request after a jQuery Ajax call
How to prevent ajax requests to follow redirects using jQuery
handle jquery ajax redirect

Maybe this answer is a little bit late but i had the same problem with 301 response on ajax request. The solution was quite simple:
apache rewrite rule is something like this:
RewriteRule ^([^/]\w+)/?$ index.php?%{QUERY_STRING} [L,E=MODULE:$1]
Your XHR-Request url looks someting like this:
/this/is/a/canonical/url + '?param=1&param=2...'
It will lead to the 301 moved permanently if you dont use a direct file call (f.i. *.php) and rewrite to canonical URL (looks like a directory-path without f.i. *.php) instead.
To solve this problem just add a / to your XHR-Request-URL like this:
/this/is/a/canonical/url + '/' + '?param=1&param=2...'
Maybe this will help someone.

I also had this problem and the suggestion about the trailing slash got me thinking ... I had a rewrite rule in my Web.Config to make everything lowercase and that's what was messing up my AJAX call. I was POSTing to GetResults (which showed up as a 301) and my rewriter (for some unknown reason?) was changing it to a lower-cased getresults GET which resulted in a 404.
Hope this might help someone else.

According to jQuery's API doc (http://api.jquery.com/jQuery.ajax/), async:false (aka. sync mode) does not support cross-domain and dataType: "jsonp" requests.

Related

Wordpress Ajax deeplink gives 404

Hi Folks,
my first post here, thanks for any help i got already throught reading before.
I am working on a wordpress projekt. And it seems i am missing the overview on my problem.
I use ajax to recieve additional product data. http:url/product/additional_ajax_data...
This works fine, except direct call of the ajax urls. Direct call of a ajax url gives
a 404 not found.
Please dont give instructions like: add 200 ok to header... Cause the project will
consist of some thousand pages and work arounds like this are a no go...
Aditional infos: the urls have no ajax hash tag... And the content will dynamicly loaded depending on last url fragment
I found the solution:
To prevent Wordpress of 404 when calling a ajax url directly, add rewrite endpoints to the system.
You can follow the post from Jon Cave on Wordpress:
http://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/
Works also on custom post_types and custom taxonomys, keep an eye on the type for wich you want to register a custom endpoint rewrite (that may depends on your options from your post type, page type etc...).
If you are sure that url is correct and file is there, check if permissions on file are not too strict. Also check .htaccess to make sure it doesn't black certain file extensions to be loaded directly

How to change domain.com/#!/url to domain.com/url

I would like to know, is it possible to change links structure from domain.com/#!/home to domain.com/home
I tried using htaccess but it doesn't change anything. I was looking for information on the Internet and found out that it is calling "ajax crawling". Unfortunately I have not found how to remove/disable/redirect.
Please help me.
You can't modify the URL fragment (everything after the #) using mod_rewrite or an htaccess file. These fragments are never sent to the server so anything on the server side is unable to access them. While you can redirect the browser to a URL with a fragment, you'll never be able to detect whether a fragment exists.
You'll need to do the redirect using Javascript on the browser's end. Check for the fragment and redirect the browser to a URL in the form of domain.com/url.

Effective way to avoid caching during Jquery Get (AJAX)

I am experiencing random occurrences of caching of Ajax requests created through Jquery's get.
The Jquery gets are done in the most straight forward conventional way (route + params + callback)
I am already using
$.ajaxSetup({cache:false});
But it doesn't seem to always work. I get how ajaxSetup no cache works, and I see the added random parameter being added to my request url.
My current browser is IE 8.0
Does anyone know of another solution besides the ajaxSetup way...
The browser itself is simply not allowed/able to cache requests with distinct parameters, as added by {cache:false}.
It sounds like the caching is happening somewhere else in your chain, possibly in your web server/app.
Use firebug's net tab to check exactly what is being requested by the browser, and what the URLs are exactly, then take it from there.
It turns out I was wrong about my assumption about caching of ajax requests.
The real issue was caching of subsequent redirect to action requests that took place on the server (in response to the original ajax call).
The solution ended up being the following attribute.
[OutputCache(Location = OutputCacheLocation.None)]
It can be either applied at the controller level or the action level.

browsers don't resend form data in ajax call if they are redirected?

I've been tinkering with URL Rewriting on my site and fixing the canonicalization of my URLs (so I don't have duplicate data on search engines, etc.) and it was recommended everywhere to either always remove/force the trailing slash (I chose to remove it) and also to redirect /index to . So I put the following two rules in my .htaccess for Apache's mod_rewrite to pick up:
--remove trailing slash
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
--remove trailing /index
RewriteRule ^(.*)/?index$ http://%{HTTP_HOST}/$1 [R=301,L]
These rules work just fine in the browser. But some of my ajax forms stopped functioning all of a sudden! I traced it both in Chrome and Firefox(Firebug) and what happens is very strange. In both browsers, the page does an ajax call to:
<mydomain>/ajax/index (ajax params intact) --> 301 redirect to <mydomain>/ajax/
<mydomain>/ajax/ (ajax params are no longer sent!) --> 301 redirect to <mydomain>/ajax
<mydomain>/ajax (ajax params again not sent) --> 200 OK but obviously script complains that params don't exist
Can anyone confirm that this is the case or am I losing it? If so, why the heck does it behave like this? And is there a proper solution for fixing it (other than changing all my ajax calls to never get redirected eg <mydomain>/ajax )?
Thank you very much for you time guys :)
Cheers
Ali
The HTTP specification says that a browser should resend the parameters along with the redirected request for a 301 redirect. On the other hand, it also says that the browser should manually confirm with the user that it's OK to resend those parameters each time the request is redirected. That kind of defeats the point of AJAX, if the user gets two dialog boxes popping up every time he/she submits your form. So for that reason, if nothing else, I'd recommend setting up your server-side scripts so that there will be no redirects on AJAX calls.
In reality, browsers tend to ignore the part of the specification that tells them how to handle a redirect on a form submission. So you're probably not crazy; it seems perfectly believable that the behavior you're seeing is built into the browsers intentionally (or maybe because someone was too lazy to code it the right way).

AJAX Calls from one subdomain to other

I want to call AJAX Request from one subdomain to other.
Say I want to request http://subdomain2.site.com/ajax/handler.ashx from a ASPX page on other subdomain http://subdomain1.site.com/Default.aspx
Any IDEA how to do this?
According to https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript, you can set document.domain to a suffix of the domain, so
document.domain='example.com'
However, I have not tested this.
have you taken a look at JSONP?

Resources