Detecting AJAX requests in vanilla NodeJS without JQuery - javascript

What is the best method for going about differentiating AJAX requests and other browser HTTP requests? I'm using vanilla NodeJS with AtomicJS as the AJAX library. It does not seem to set the 'X-Requested-With' header, which I read was used by JQuery to indicate AJAX requests. Should I switch to a library that does? Is there another way to detect AJAX requests? Should I just attempt to set the header manually, and are there any potential issues involved in doing so?

There is no reliable way to tell the difference (short of you explicitly setting a header or other indicator on all of your own requests). More importantly, it should not really matter how the request was sent.

You could explicitly state that the request was coming from an Ajax request in the header or in the body of the request, but likely the better way to do this would be to have a separate endpoint on the server for an ajax request.
For instance your uri for an non ajax request might be
http://myserver.com/non-ajax-uri
and then you could have another endpoint at
http://myserver.com/ajax-uri
If most of the behavior is shared between the two endpoints simply encapsulate it in another function that both of the end points share.

Related

Browser to issue new HTTP request based on response from another HTTP request

I'm looking to dynamically issue a new HTTP request from the client/browser based on response from an earlier HTTP request, in the same page load. Basically, the flow would look something like this:
Issue HTTP request to www.site1.com/fetch, and parse response to get some string mydata.
Issue HTTP request to www.site2.com/lookup?key={mydata}, substituting in the data just obtained. This call should be done in the same page load.
This is related to HTTP redirections, but it's done on the client-side with more flexibility, to possibly a different domain.
I think client-side JS is the best way to achieve this (perhaps with some kind of callback), but I'm open to other ideas.

What is the difference between an JSONP request and AJAX request?

I've been digging for an answer to the above solution, but I did not found something interesting.
Can you guys please explain it in clear words??
Ajax is a generic term that means "Making an HTTP request and processing its response, from JavaScript, without leaving the page".
JSONP is a specific means of making an Ajax request that is performed by adding a <script> element that loads an external JavaScript program that does nothing except call a specified function with some data as the first argument. It is a hack to get around the Same Origin Policy. (It is obsolete in the face of CORS which provides a much safer, and more refined, means to do the same thing).

AJAX catch difference between «failed» and «canceled» sending request to another domain

I'm making ajax request to server on another domain but I don't actially need its response, just to know that it got my request.
When everything is ok, in Chrome Developer Tools (Header status) it says «canceled» and console writes «XMLHttpRequest cannot load» but server gets my requests.
When server is down then header status is not a number but just «failed».
Trying to catch this critical difference on JS I get XHR status 0 in both cases.
I'm making ajax request to server on another domain
You can't make an ajax request to a different domain, due to same-origin policy. You want to look at JSONP, which essentially writes out a <script> tag for a remote URL.
What is JSONP all about?
Detecting success/error with JSONP calls is tricky, it doesn't work like typical ajax calls at all. Ideally you want the remote server to call a callback function on your page, as the above link describes.
If you don't control the other domain, you can attempt to detect errors by using a timeout. Here is a post discussing the jQuery timeout argument for this, though you could certainly implement your own timeout with raw javascript as well.

Cross domain request to localhost from a BHO injected JavaScript (in IE)

I have a working prototype of a BHO (simple C# with COM interop) which injects JavaScript to any page which is loaded by IE. Simple scripts are working like console.log(..) etc.
My goal is to make AJAX requests from the injected javascript to localhost. I understand it's an X domain request, but can't figure out how to implement it.
I carefully read about XMLHttpRequest and XDomainRequest (both gives me Access Denied)
Btw I would like to send/receive JSON and XDomainRequest allows only plain/text (why?, surely not security issue enabling plain/text but not allowing json...) Also I've read XDomainRequest does not allow localhost.
I understand that response header should allow X domain with 'Access-Control-Allow-Origin: *' (Is this the response of the page I am injecting into?)
Maybe I can modify/patch the response header to allow X domain from my BHO? Is this possible?
As a last resort I considered make the request in my C# BHO. However this means to call from javascript to C# (maybe it's working) but the main problem that this call is a synchronous call. Even if I issue the request in async way in C# in it completion handler then I must call back a javascript completion handler. This sounds as terrible overenginering calling from javascript to C# then calling back from C# to javascript not talking about the robustness and perfomance loss.
Any ideas?
Thx in advance

Cross-(sub)domain AJAX POST request (with file/large body)

I need a script to perform a POST request to a different sub-domain than the one the page loads from, e.g. load data from domain.com and perform the AJAX POST to post.domain.com.
I've read about some alternatives that work for mainly for GET or POST with simple form data, but in this case I'll be posting a file (can be quite large).
I control the server, and both the page and the target are under the same domain. Is there any way to do this with JS/Iframes or do I have to resort to Flash/Flex?
As a side question, does mod_proxy for apache redirect a POST when the HTTP request is fully read (at apache) or it starts redirecting traffic (like a TCP tunnel) as soon as the headers are read?
Maybe Why am I getting an OPTIONS request instead of a GET request? can help you.
For requesting data from another subdomain you could look at JSONP
For posting files you can definitely use iframes.
This is a good tutorial: http://www.openjs.com/articles/ajax/ajax_file_upload/

Categories

Resources