I searched over and over again, but maybe my search terms were incorrect, because nothing turned up. I want to dynamically show my 404 component depending on the results of an HTTP request.
I realize that I can do this by redirect the user to my 404 page, but is there any way I can render the component without changing the URL?
Something like /people/<nonexistent id>
Perform HTTP request
If person exists, proceed as normal
If person doesn't exist, render 404 without changing the URL
Thank you!
Never mind, I found the answer.
The solution is to userouter.navigate with the option skipLocationChange set to true. This tells Angular to reroute without changing the URL in the browser navbar.
https://angular.io/api/router/NavigationExtras#skipLocationChange
Related
This is a hypothetical question. No codes whatsoever.
Say that you should figure out if two URL's navigate to the same page. You have to do this programmatically. Just scanning through the URL is not enough, maybe one of them has an anchor link which navigates to the same page but different section. Or maybe there could be a ref=... on the url that is just used to monitor the referrers by the backend.
One possible solution could be requesting the contents of both URL's and comparing the HTML outcomes, but if you have to do this many times to many different URL's it is a costly operation. Could there be a better solution for this ?
Thanks
Sure, by reading the HTTP response headers, which are sent by the server after each request.
When you're redirected, the server responds with the status code 302. 200 is the code when everything is OK and the most popular is the 404, not found.
Check this Wikipedia page for a little more about the 302 status code and this one for the complete list.
I am looking for a way to simply disable AJAX on ONLY the home page in my project. I am using MVC so there are links to the home page in many areas. I want AJAX for the entire site however not when loading the home page. The reason is because I am using a script to setup the UI dynamically, it contains objects and those objects load when the page loads as a hard refresh but no when jQuery Mobile transitions to the page. I am looking for a solution where I can add a Script to just the home page to disable AJAX whenever it is referenced. But still have AJAX for the rest of the solution. Its a very unique situation but I haven't found anything that could prevent ONE page from being loaded through AJAX. Does anyone know?
if I could I would like to do something like this only on the home page
<script>
$(document).bind(function(){
[some call to disable this page from being loaded with AJAX]
});
</script>
Thanks in advance.
No such thing exists. Think of AJAX like a little mini-browser: if the URL returns a response, it can access it. However, it might be possible to branch on the existence of a request header in your Home action and return something like a 403 Forbidden if an "AJAXy" request header is detected.
jQuery, for example, sends an X-Requested-With: XMLHttpRequest header along with its request. If this header is present, the request most likely came from an AJAX method in jQuery (or other libraries that potentially use the same methodology). You can also pass your own custom headers with the AJAX request, if you want. However, all of this is implementation-specific, so it pretty much depends on all developers on your team doing things a certain way. For example, if someone used a standard JavaScript XHR object to make the request, then the X-Requested-With header would be absent. Or, if you used a custom header, every developer would need to pass that header every time, or again, the logic would break down.
If your business rules say that the home page shouldn't be requested via AJAX, the best way to handle that is at a business level, i.e. policy. Anything you try to in the actual code could easily be worked around.
I need some ideas i am using url rewriter for seo purposes.. maybe I shouldnt be doing this i am not sure if you have any comments on this let me know
I am making sure that all requests get directed to www.mydomai.com for
www.mydomian.com/home and www.mydomain.com/home/index as there all the same page but with mvc obviously you could get to them by all of these urls.. I am thinking this could cause duplicate content issues with seo so i wrote some rules
This works fine any request to any of these urls redirects to www.mydomin.com
the problem that i have is that i have a partial form post that updates to the home controller It will not post back as in the net panel it says url permenatly moved I am guessing this is to do with my url rewriting. Any ideas
If anyone else has this problem I have fixed it... I am using the url writer to make urls friendly ie making them all lowercase to avoid duplicates and taking the trailing slash off to ensure that I have no links that are slightly different going to the same place.. Forms do not like this if you call them with capital letters in the names.. It initiates the form via ajax but when you go to update it says 301 removed in the net tab for updatepost. You need to ensure you call them with lower case controller and action..
I have a referral project.
In that there is an agent between the
affiliate(my site) and the target site.
So all hyperlinks displayed in my
site will be redirected to the agents
site and then it is redirected to the
target site.
The hyperlink itself is the link to
the agents site.
So i am displaying all products. I
have given links to them. When users
hover a hyeprlink you can see the
href text displayed in the status bar
which will be the agents site.
The requirement is the hyperlinks of
an agent site should not be displayed
in the status bar.
My solutions and assumptions
So i can use span onclick =
window.location = hrefOfAgentLink.
I have seen in other sites that they
redirect to another scripting page
and then the redirection takes
place. for example
http://sitename.com/click/id=32.
The id refers to the hyperlink in
database. hope they fetch the link
and they do a location header to
redirect to the agent page.
Why so? because the user should not see where the link goes.
I want to know whether there is(are) any other option(s) so that the hyperlink will not be visible in the status bar and in the address bar when it is redirecting.
Anyway when redirecting the agents url is not visible because it immediately redirects to the target site.
I would like to have the stackoverflow users suggestions. Thank you.
You used to be able to set the status bar message using javascript, but you can not do that reliably anymore. This is most probably because the browser wants to protect the user. Instead of trying to hide the URL, perhaps you should work on a "redirect-URL" that looks reliable and that makes it obvious to the user both that it is a redirect-URL and where it is going. For example this kind of URL would both make me aware of where I am going and that my access is going to be recorded:
tracker.my-ad-network.com/stacktrace.com/ad/568
Instead of something like this (that would not make me feel safe):
dashj2.gggfbad.com/index.php?aid=1232808432&ref=123432.
And as noted above, start accepting serious attempt to help you or people will simply stop :)
Piotr
You can use the new data attribute so your code would look something like this:
link text
Then in your JS, if you're using jQuery for example:
$('a').click(function(e){
e.preventDefault();
var loc = $(this).data('redir');
window.location = loc;
}
Not too hacky for what you're trying to accomplish.
I have a pager on a table using ajax and I would like each such request also to change the browser's url, so when I hit refresh button I won't skip back to first page. I was fighting the Url parameter of AjaxOptions, but it keeps winning over me. Please help.
Trim
You can safely change the URL past the hash mark without redirecting the page. However, the user can (in most browsers) navigate through these changes with the Back and Forwards buttons. This technique is usually called "history."
Because the technique is difficult to get working in all browsers, you'll want to use a framework. Take a look at http://www.mikage.to/jquery/jquery_history.html.
I can also recommend ExtJS's history stuff too. Take a look at this example:
http://www.extjs.com/deploy/dev/examples/history/history.html#main-tabs:tab2
Again, notice that not only does the URL change when the user does stuff, but changing the URL (via Back and Forward) also affects the page. This is good, awesome even, but means it must be done very carefully.
There is not really a quick and easy way to do this, here is an article on the topic. The problem is that not only does the Ajax have to generate the URLs, it also has to take those URLs into account when loading the page to get the appropriate content.