Calling an http request in an window:unload handler in Angular 5 - http

I have an application which needs to make sure the user has saved their data when they accidentally close the browser (or tab). I have added a window:beforeunload handler to show the confirm dialog and a window:unload to call an http service call if the user really did want to leave but save the changes. However, I the call never gets executed on the server (unless I set a break point chrome). I found a post using angularjs (How to send an HTTP request onbeforeunload in AngularJS?) but cannot for the life of me figure out how to do this in Angular 5. Any help would be much appreciated.

I have figured it out with a bit of help from a different post (Angular 2 - Execute code when closing window).
So to save my info I use a post like the following...
let xhr = new XMLHttpRequest()
xhr.open("POST", url, false);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));

Related

Go urlfetch.Transport.RoundTrip, can call in GET, but not POST?

I'm having a problem getting any response from urlfetch.Transport.RoundTrip in GAE Go. When I browse a page that makes the call in a browser, the call is executed as intended. When the same function is called from a POST request made by poclbm Bitcoin miner, I can't get a response.
The call is made by this package I made at line 77.
Is it possible, that in Google App Engine one can request data from other web pages under a HTTP GET, but not POST, or is there something else that can be causing this problem?
You can do POST request from App Engine using http.Client.Post, just make sure you create the http client with urlfetch.Client function.

Partial PostBack without AJAX

I want to silently check for Session existence without Posting back page by using AJAX Timer and AsyncPostBack Trigger.
Now, I would like to know is there anyway to silently check whether ASP.NET C# if (Session["email"] = null) { Response.Redirect("Logout.aspx"); } something of this kind to check for every 10 seconds without Posting Back Page and without using AJAX by using something like jQuery or any other technology that is supported by .NET?
The server can push to a loaded page if you use an asynchronous controller with a partial view which is loading every set amount of time.
http://msdn.microsoft.com/en-us/library/ee728598.aspx
there is only one way to get to the server. send a request. that can be a "standard" request where the browser will refresh the screen when a response is sent. the other type of request is a ajax request. the difference is an ajax request contains a header to inform the server it's an ajax request. when the browser receives the response it will allow the developer to decide what to do with the response. either way a full request/response exchange takes place the only difference is how the browser handles the request.
jquery is a javascript library that includes functionality to make ajax request easier to setup.
to answer you question no, there is no way around making a request.

NodeJS Express DynamicView Helper with an http request

I hava a node.js app running on express with a dynamicViewHelper that hits an http service with http.request. Unfortunately, when I try to do this, because the http request is asynchronous and the function calling it is not, the page just renders before the http request is finished and I end up with undefined on my page.
I am basically looking for a way to put an asynchronous action (the http request), inside of a dynamic view helper which gets rendered out on the page. I know I can't just turn it into a synchronous function, so I am looking for another way to get that dynamic data to the view.
Also, is there a way to cache the data that is returned from an http.request()? I don't want to put it on the session, but I want any further requests made to the exact same URL to possibly be cached... Not as big of a deal as the first part of my question, though.
I figured it out. I'll just load it in my route so that res.render() isn't called until the request is done.

AJAX callback locks the page

I have a long-running database query that I've placed in an asynchronous AJAX callback (or so I think) to allow the user to navigate to another page if they're not interested in the results.
Whether I call the query automatically after page load or on click, the page always locks up until it returns, i.e links and buttons don't work. Internet Explorer 6's own menus are fine however, so it's not that IE itself is overworked.
What could I be doing wrong? It seems as though my request isn't really asynchronous. Here is a code snippet:
this.XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
this.XmlHttp.open("POST", url, true);
this.XmlHttp.send(data);
It's not the browser that blocks the page, it's the server.
IIS only handles one request at a time from each user. As the user already has a request from the AJAX call that the server is working on, the new request will be queued until the first request is handled.
To keep the long running request from blocking the user on the server you have to make the page that the AJAX call gets session-less. That way it's not associated to the user.

How do I send a HEAD request manually using Firefox?

I'm debugging my webserver, and I'd like to manually send HEAD requests to some web pages. Is there a way to do this in Firefox? Some extension perhaps.
I want to use firefox so that it can be part of a normal session (ie cookies set, logged in, etc). So things like curl aren't perfect.
Another possiblity is opening up firebug (or making this into a greasemonkey script) and using javascript to send your HEAD request.
// Added comments
var xmlhttp = new XmlHttpRequest();
xmlhttp.open("HEAD", "/test/this/page.php",true); // Make async HEAD request (must be a relative path to avoid cross-domain restrictions)
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) { // make sure the request is complete
alert(xmlhttp.getAllResponseHeaders()) // display the headers
}
}
xmlhttp.send(null); // send request
XmlHttpRequests inherit the cookies and current session (authentication from .htaccess etc).
Way to use this:
Use the javascript: url method
Use the Firebug console (http://getfirebug.com/) to execute javascript on the page
Create a greasemonkey script that executes HEAD requests and displays the result
Live HTTP Headers can send arbitrary HTTP requests using its replay function. Though it's a bit fiddly. And as it's a HEAD request, there'll be no output to see locally (it's normally displayed in the browser window).
First you need to open up the Live HTTP Headers (LHH) window, do your request from the browser using GET, then select that request in the LHH window and choose Replay.... Then, in the window that pops up, change GET to HEAD and fiddle with the headers if you like.
Pressing Replay will make the request.
This is a pretty old thread, but there is a firefox plugin called "Poster" that does what you want.
There is another plugin I've used called "Rest Client" that is also good.
I don't know of any plugin but this page might be of some use to you
http://www.askapache.com/online-tools/http-headers-tool
I believe that you can send head requests with Fiddler
http://www.fiddler2.com/Fiddler2/version.asp
This seems to be a solution that works in firefox as an addon, called Modify Headers
https://addons.mozilla.org/en-US/firefox/addon/967
Check out http-tool for firefox ..
https://addons.mozilla.org/en-US/firefox/addon/http-tool/
Aimed at web developers who need to debug HTTP requests and responses.
Can be extremely useful while developing REST based api.
Features:
* GET
* HEAD
* POST
* PUT
* DELETE
Add header(s) to request.
Add body content to request.
View header(s) in response.
View body content in response.
View status code of response.
View status text of response.

Resources