I have application with huge ajax. Some times ajax not working properly when user close browser. This happens because there are many 3rd party add on installed in their browser which stop (may be) work ajax properly.
Now I have no control over users, so unable to make users careful about their browser. Now what can i do in this situation. Is there any way to force ajax to work even with bad extension installed in browser?
Following code not work when bad extensions (like ask toolbar,alibaba news ticker etc) installed in browser
window.onbeforeunload = function () {
$.ajax({
url: site_url + '/agent/send_offline',
type: 'post',
cache: false,
async: false,
...
});
};
You cannot rely on always being able to do an ajax call when the user leaves your page. That just isn't something you can reliably do in a browser. It may work sometimes, but is not guaranteed to work in all possible ways that a web page can be closed.
There are some work-arounds:
You can maintain a websocket connection from your page to your server and when the user leaves the page, the socket will be automatically closed by the browser and your server will then know the user left the page.
You can have your page poll your server every few minutes with a simple ajax call and when the server no longer gets a polling ajax call, then it knows that the page has been closed.
you can do it by storing cookie in browser to call your page after every miniute where ajax code is placed. try this angle
Related
I have a nightmare of a Laravel 4 issue.
Anytime and every single time an AJAX POST is made to the server in the app, it immediately logs the user you of the system! This is horrible and impossible to debug ti seems
Does anyone have any ideas for a solution?
Below are some examples of the AJAX request being made...
$.ajax({
type: 'post',
url: '/orders/orderboards/order/add-item-comment',
data: 'order_item_id=' + order_item_id+'&name='+name+'&body='+body+'&user_id='+user_id,
success: function(result) {
if(result.success){
console.log('SUCCESS AJAX Comment created: ');
working = false;
$(result.html).hide().insertBefore('#addCommentContainer').slideDown();
$('#body').val('');
}else{
console.log('FAILURE AJAX did not save comment:');
}
}
});
I also use the jQuery library X-Editable on some fields of my app which turns fields into edit in place and it handles all the AJAX side of those posts as well.
I can use an AJAX request to load data without issue, it is just when a POST is made to create or update data that you are logged out.
UPDATE
It seems anytime an AJAX request is made to server it is resetting the laravel_session cookie value. I am not sure what would cause this.
I have noticed in the past if I turn on the config debugging option then each request also resets this value and logs you out. It seems AJAX request also make this behavior happen =(
Any ideas for a solution?
This was a very super strange issue which I still do not know the reasoning for it however I do know the cause now...
My JavaScript app made an AJAX request to build the content of a Modal window. In this generated HTML, it made a request to get a Gravatar image and when the image was not a correct URL to a real gravatar image, it forced my user to be instantly logged out of my Laravel 4 app.
I have no idea why that would do anything my my server. Would love to hear some reasoning?
I want to know is there a way correct way to delete user from online users list with $(window).unload jQuery function?
or keep using PHP to check it?
i think this is not a correct way because user can edit it, but i need more reasons.
Code example:
<script src="jquery.min.js"></script>
<script>
$(window).unload(function() {
$.ajax({
type: "GET",
url: 'http://localhost/delete_session.php',
data: "id=" + "session id";
});
});
</script>
Thanks!
Doesn't work well with multiple tabs : if your the user opens your app in two different tabs, closing any of the two tabs will delete the session, and the other tab will become "unusable".
You should not solely rely on the client's behaviour to manage your sessions : the client's browser may exit incorrectly (program bug, battery out, kill -9, etc...) and not execute the unload callback on exit.
A correct way to do online list is probably using heartbeat requests.
Your javascript sends small requests to server indicating they are "alive".
If all pages are closed by user, the heartbeat stops and you somehow remove it from online user list on the server.
I am trying to create a website where you will remain logged in via a session in php as long as you stay in the website but will be logged off if you leave the website. Example, you would remain logged in when moving from mysite.com/index.php to mysite.com/otherpage.php but would be logged off when you move to othersite.com. Is this possible?
So far I have tried using ajax and jquery to end the session when the page is left but this also ends the session when the page is refreshed.
$(window).unload(function(){
$.ajax({
type: 'POST',
url: 'logout.php',
async:false,
data: {key_leave:"289583002"}
});
});
There is no possibility to detect whether user left your site (imagine scenario when user just disconnects from internet, but browser is still open, or close browser via KILL), so basically you cannot do what you want.
Anyway here is some clues:
Why do you need it at all? What is the goal? Try to re-think your architecture.
You can configure session.cookie_lifetime to 0, this will mean "until browser is closed"
You can add some javascript "ping" on each page, which will call your script every 10 seconds for example, or stop calling when internet is down, user went to another site and no pages are in other tabs, then you will need separate cron job which will destroy sessions for users without pings last time
A Simple solution is
a) On the server side - If a session exists then retrieve from the database (on the server) the last time the user accessed a page. If too long ago get them to log in again. Otherwise deliver the page and undate the database
b) For a better user experience use Javascript to update the page (setTimeout).
If (b) does not work (i.e. for whatever reason) then at least the server is protected along with the individuals data
So I have a bookmarklett that seems to not work on some sites.
Here's what happens. The bookmarklett (exists on your bookmark bar) fires a javascript sequence that will construct a div with content on any page you are currently on.
Once the div is constructed, it will send an ajax GET request to a php page on my server (that is, the page the bookmarlett is loaded on is not within my server, this is somewhere else, ajax requests data from my server to be sent to the website the bookmarklett is loaded into).
Apparently, I have not seen this error myself, I was told that the ajax call failed to be received, so no dynamic data was passed into my constructed div.
The order of events is as followed:
Bookmark Clicked -> Creates a modal popup div on the page being viewed -> runs an ajax call to my server, passing a unique ID of the user to retrieve some data -> then populates the dynamic content with this retrieved data.
Apparently on some site the ajax request was never received back.
Can some sites block my bookmarklett from gathering data via ajax from my site?
Is it possible that the site messed up the request being sent to my server for a response?
If so, is there any work around? Or do i need to ditch it all together.
This is how my ajax request works, and I have not seen it fail, but apparently it has.. It may have been an error on the users part (may have touched some of the code supplying the data to send with the GET request, I'm not sure though)
jquery.ajax({
type:"GET",
url:"mysite.com/api/getStuff",
data:"format=jsonp&userid=1234",
success:function(data){
// do stuff with the retrieved data
},
error:function(err, msg){
// response was not received
}
});
XMLHTTPRequest is subject to the same origin policy which is imposed by most browsers. For security reasons, it restricts XHR to the same protocol, domain, and port as the original page request.
Visiting the link I provided will also start you on your way to finding workarounds for this if it's something you want to do.
Crossdomain ajax requests are restricted. Use 'jsonp' or 'script' dataType.
Note that "Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation". More info is available on official jquery documentation
I know from experience that some mod_rewrite rulz messed up my ajax requests, i think you might have some problems with using ajax + mod_rewrite.
when i update score from my admin i want to update score in client autometically without refresh can any one help with script and technices
i see such auto refresh https://stackoverflow.com/
ANSWERS , VIEWS autometically updating
The only way I know to do this is to actively poll your server from the client.
You'd need to create some polling script that called your script service page with a request for the data. Then, when the data is saved in the admin page, the service will return the latest scores when next asked.
Typically, you'd use setInterval and clearInterval. So on page load, you'd assign your polling function call to the setInterval method and give it a timeout of something sensible (10 seconds plus depending on how often you expect to update your scores and how big your traffic is).
You'd need to be using clearInterval whenever you want to stop the polling. As per the other answer, a JS framework will help you with making the Ajax requests whether in Xml or JSON format. Given your tags imply 'faster' then I'd recommend JQuery and JSON.
Additionally, whatever framework you use, consider using Googles CDN for fast deployment of that framework.
I'm unaware of any server push with Ajax so would be interested to see if that methodology is available (though I doubt it).
EDITS: added more info on Google cdn and frameworks.
HTH,
S
You'd probably want to look into a js framework.
The steps you'd go about doing this are similar to:
Add an onclick listener to your link /button
When that gets trigger, stop the event (this way you stop the browser from refreshing / redirecting the page)
Get the user submitted data and send it to the server via an json ajax request
Process data server side and return a response
Process the response and update the interface accordingly
Here is a bit more documentation on how you could do this with mootools:
* http://mootools.net/docs/Request/Request.JSON if you want to go with the JSON path
* http://mootools.net/docs/Request/Request.HTML if you just want an AJAX updater
While I haven't used it, it sounds like this is what "comet" is intended to solve. It's a way of streaming content from a server to a client - basically a "push" approach. It might be what you're looking for.
http://ajaxian.com/archives/comet-a-new-approach-to-ajax-applications
There is no need to refresh the page if you use AJAx.
With AJAX you can call the server and place the result in a portion of your web page without refreshing the page.
There is an article here regarding using AJAX via jQuery.
The jQuery documentation on AJAX is good too.
An example which would request data from the server and append the result to a section of your page is shown below:
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
$("#results").append(msg);
}
});