Disable AJAX on only home page with jQuery Mobile - asp.net-mvc

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.

Related

Transferring Result Data via HTTP Header? - Asp.Net MVC

I am trying to upload file without refreshing the page.
I got 1 form, 1 submit, 1 file input and 1 iframe in order to prevent refreshing.
Form sending data via iframe, so my form have target attribute.
After my c# function's work, I want to return result data, such as message, issuccess etc.
I don't know how to return result data without using http header.
Maybe it's also not possible with http header. I don't know. I am here to learn how to do.
Transferring result data via http header makes sense? Is it preferable way?
Does it occurs vulnerability?
Any other suggestions?
Thanks in advance.
Typically it's hard to get a file over the wire without a form post.
What I've done often is use an invisible iframe for a form post and then have the iframe call a function in the parent page upon load. This assumes you can't just use jquery or a recent version of dojo to take care of this for you.
http://viralpatel.net/blogs/ajax-style-file-uploading-using-hidden-iframe/
If you can use jquery it's much nicer. Edit: this is under the mit license
http://blueimp.github.com/jQuery-File-Upload/
Why not take advantage of the HTML5 File API. These links should point you in the right direction.
http://www.html5rocks.com/en/tutorials/file/dndfiles/
http://timothypoon.com/blog/2011/05/10/ajax-uploading-with-html5s-file-api/

How is this URL modification possible?

Could anyone please tell how the site http://www.outsharked.com/imagemapster/default.aspx?what.html is working in such way? Modifying the url without loading/reloading the page. I think this is not done by html5. Because it works in IE6 which doesn't support html5.
I created that site. The commenter is correct, it uses Javascript to change the URL. There's nothing about how that navigation works that is different for IE6 - that browser supports the necessary client-side functionality to do this kind of thing. The basic functionality involves:
capturing click events on the nav, and loading the inner content via AJAX
update the URL to reflect a working direct URL to target.
The links also are valid anchor links that, in the absence of Javascript, would go to the same page (but load the whole thing). This is your basic AJAX web site setup with one minor difference. It's common practice to use a URLs like this in AJAX/single page web sites:
http://mysite.com/home#somepage
or even just
http://mysite.com/#somepage
Where the hashtag part represents the actual page a user has navigated to. If someone accessed that url directly, e.g. from outside the site, the site would use Javascript to load the correct content based on the hashtag, after the page had loaded. This means that there might be a little delay for the inner content to reflect the correct page, since it has to run another request after the initial page has loaded from the browser to get the inner content via AJAX.
I was trying to avoid that by creating a setup that worked completely with and without Javascript. If you go directly to a URL within the site such as http://www.outsharked.com/imagemapster/default.aspx?faq.html you will notice it loads the content directly. This URL will work even if Javascript is disabled. You can't actually do this using hashtags, since hashtag content is not sent to the server. Only the client knows what's after the hashtag in a URL. That's why I was using query strings to represent inner pages.
This site architecture was sort of an experiment at the time. It works pretty well but the code isn't fantastic, I didn't really do anything else with it, and I'm sure there are other better-fleshed-out/tested/full-featured frameworks out there to do much the same thing.
But it might not be a bad example of the nuts and bolts of creating a basic AJAX navigation setup, as a learning tool, since it's pretty concise, and also does HTML5 history navigation (e.g. so the back button works on modern browsers).

Get POST response data within Greasemonkey script

I want to get reponse data send by a Jetty Server via POST within a Greasemonkey script.
New data is send every few seconds.
How can I directly access the data, that is displayed in Firebug "Network" tab as response data?
(I tried to get the data with solutions provided here that make use of jQuery and ajaxSuccess, but that did not work. And I am not sure if the POST data is returned from an jQuery ajax request. So is there a way to directly access the POST "stream").
The best way depends highly on details that you've not (yet) provided:
Exactly how does the page send and receive the information. Is it really form posts, or is it AJAX? If AJAX, is jQuery involved? Pages can post without AJAX; are iframes involved?
Link to the target page.
What does the page do with the information? Does it update displays?
What is your ultimate reason for wanting the information? Most of the time, it's easier not to try and intercept -- and parse -- AJAX. Usually, monitoring the DOM tree is much easier and more robust way of getting what you want.
If the page displays the data, you want, to the user, the best way to get it is shown in "How can I detect AJAX node insertion...".
If you really want to intercept the data:
Intercepting jQuery AJAX from Greasemonkey.
Intercepting any target-page AJAX from Greasemonkey.
Intercepting AJAX, bare bones.
If the page is submitting forms, creating iframes, etc., POST DETAILS. But in general, you would replace the javascript function that is receiving (and/or sending) the data.

Umbraco - different behaviour for usual and ajax requests

I'm developing an Umbraco site that is a "single page" - no reload, only ajax calls.
The site will have nice urls and use html5 push state history.
The problem here is that every time a request is made to the server I need to handle it differently depending on the type of the request: normal or ajax.
For usual requests I need to display the content along with it's master page.
For ajax requests I need to display only the content.
I don't know how to accomplish this - routing and master page magic.
Can anyone help?
You could use alternate templates. For more information see here. Basically, have the alternate template just render out the content in whatever format you want, without the full html template, and then make sure that all your AJAX requests call the pages using the alternate template.
One word of warning though, if you're doing all the site navigation with AJAX and no page reloads, then Google (or most other search engine spiders for that matter) won't be able to index your site properly (as they don't process javascript) and your site won't rank very well.

ajax request changing url

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.

Resources