jquery ajax asynchronous requests error - javascript

at the beginning I would like to say sorry for my bad english.
jquery v2.0.0 in Google Chrome, Mozilla Firefox, Opera last versions
Today I had a problem
timer_multy_update = setInterval(
function()
{
$.get(
'test.php',
function (result){
parseAndUpdateData(result);
},
"json"
);
}, 500)
The problem is that if the server hangs (I don't know how to say it correctly), i.e. time to get answer from the server more the 0,5 second, but timer not stay and continues to send request, so before the server answer it can be send 2-4 request, all this answer return for a little time and, now a problem, in firebug all request correct, but variable result contains only one answer from the first answer from the server. maybe I did not express myself clearly, I want to say that 2-4 request to the server return different answer, but in result gets all 2-4 times the first answer from the server, and it is big problem.
I tried to find information on the Internet, but found nothing.
I do not know why but the first thought was that the error in jquery, I began to look at the source code, and found some mention about heder and it's hashes.
So i try to change my script and find to way
$.get
(
'/php/mine/update_cells.php',
't='+Math.random(),
function (result)
{
parseAndUpdateData(result);
},
"json"
);
it works correctly
so I want to now, bug it is or my mistake and not understanding

This isn't a bug, it's caching. It's a lot more efficient for a browser to cache a resource for a while then have to go and get it every time someone wants it. This is fine for static resources, i.e. ones that don't change much but for a web service that returns different results frequently for the same URL you will want to disable the caching. If you control the server side code, add a Cache-Control: no-cache header to the response. You can disable caching in your jQuery but as far as I know, you have to use the ajax() function - there's no way to do it with get().
$.ajax({
url: "/php/mine/update_cells.php",
success: function(result){
parseAndUpdateData(result);
},
cache: false,
dataType: 'json'
});

Related

How can I "ping" the user in JavaScript?

I need to get the request time it took the client to reach the server and the opposite.
And so I have been able to do this in Python which does not help me now on a webpage.
So I heard that it can be done using Ajax, is that true?
If so, Can you give me some details or information where I should start from?
Thank you, looking forward to a answer!
You could emulate a ping via http however that is not very accurate. A simple way would be to post the current timespamp in ms or ns and wait then for the response of the same time. The difference between the real time and time of the response is the so called round trip time (RTT). If you divide it by two you get the response time what the ping is.
On newer browsers, you can use the navigation timing API (HTML5 rocks article on navigation timing) to get a fine-grained break down of the time that it takes to load your page. You can simply subtract the relevant fields from "performance.timing" to get the timing that you are interested in. The (secureConnectionStart - connectStart) or (connectEnd - connectStart), depending on whether the connection is an SSL connection, looks like a reasonable approximation of ping time (though, as rekire# points out, you probably want to include more than just that if you are trying to measure the overall user-visible latency of your website for end users).
For your reference, the ajax ping works as below.
Unfortunately it is very possible you will get blocked by domain policy and never get a success.
function ping(){
$.ajax({
url: 'http://website.com',
success: function(result){
alert('replied');
},
error: function(result){
alert('error');
}
});
Please see this code as an example of a seemingly successful implementation for what you are trying to do.

Intermittent truncation of AJAX requests--how to evaluate and/or workaround?

I have a JavaScript web app that makes numerous AJAX calls (via jQuery), the largest of which posts between 50kb and 250kb. For these larger requests, the payload is a JSON string, and I have found that a small percentage of the time, the request is truncated. It arrives at the server, and the server throws an exception because the JSON string is broken off right in the middle.
From reviewing our logs, I can see that:
It happens with many different browsers (e.g., Chrome, Firefox, IE, etc.) on many different platforms (e.g., Windows 7, OS X, iOS, Android, etc.)
The user are able to complete their submissions. In other words, it looks like they try again, and the same data goes through on the second try.
The Content-Length header of the request is larger than the length of the request string actually received by the server.
I have never been able to reproduce this problem. But it's an ongoing issue that happens multiple times per day, and if there's any way to address it, I'd like to do that.
I'm not even sure how to troubleshoot something like this, because it's exclusively happening on a remote client. Any suggestions? And has anyone else experienced this, and were they able to determine the problem?
Here's the JavaScript that's generating the calls. It works 99% of the time, and usually (always?) the user can re-initiate the request (thus executing this call a second time) and the entire request goes through;
var token = $.ajax({
type: "POST",
url: serviceEndpointUrl,
data: requestData,
dataType: 'json',
timeout: 9000000, // 15 minute timeout
});

How to hide details in jquery ajax from browser page source

I am using jquery for all my ajax thing, I don't know if that is fine but I use that for now.
I have one text input when user type characters in it I call server side get some values and add them on the view.
Code that I use bellow works fine but I want to improve it a little.
How can I make this ajax call so that users that want to investigate my page source code can't see what I call here?
So basically I want to hide from page source what url, what type and data send I use here, is it possible?
$(function () {
$("#txtSearch").keyup(function (evt) {
$.ajax({
url: "/Prethors/Users/SearchUsers",
type: "POST",
data: "text=" + this.value,
success: function (result) {
$("#searchResult").prepend("<p>" + result + "</p>");
}
});
});
});
No, a user will always be able to figure out what calls you are making if you include it in javascript.
You can compress and minify the javascript, but a determined person will always be able to find your url calls.
Here's a js compression site, for example.
http://jscompress.com/
overall, you shouldn't worry about this. there is no way I'm aware of to hide your ajax calls, but you shouldn't need to.
-you could encrypt the info.
-you could use comet to stream the data on a persistent connection. (super complicated).
-follow good server security practices and not worry about it.
source: here
If you are really worried about this, you could set up kind of an anonymous URL, which will then redirect to where you really want to go based on some variable which is arbitrary.
for example, instead of going to "/Prethors/Users/SearchUsers"
go to "/AnonymousCall?code=5"
from which you could execute the code you want for searchusers
You can't hide client-side code. You can disguise it with minification but sensitive data should always be stored and processed on the server-side.
Use console.clear(); after you ajax calls :P
It just clears the reqs from the console but you still cannot hide client side calls.

Downloading LATEST version of a page with JQuery/JavaScript?

The answer on Auto refreshing with Javascript? seemed like exactly what I needed, but after a while I found something not working like I wanted it to :( When I made a timer to check the web page every 5 seconds, it kept returning the same string even after the page changed. I think this is happening because it's doing the equivilant of F5; re downloading the page only if the php script has been changed by me, and if not just sending my Javascript what's in the browser's cache, if that makes any sense. The problem is, the page isn't actually being re-uploaded every five seconds, the reason the page would change is because of the database content the PHP is displaying. What I would like is a function similar to $.get but will act more like Ctrl-F5, not using any cache, just re downloading the whole page. Sorry if this doesn't make any sense...
UPDATE: What I'm asking for is not a Javascript script that Ctrl-F5's the page, what I'm asking for is a function like $.get that downloads from the server no matter what. urgle, see, $.get only downloads from the server if the page has been edited since x time (and if it hasn't it'll return a copy of the page from the browser's cache), but I don't want it to do the x time thing, I just want it to download the page no matter the last-edited time.
I always throw useless query like this:
$.get(url + '?v=' + Math.random(), success: function (data) {
// stuff
})
That math.random basically tricks the browser into not caching that request.
Use jQuery.ajax(). Pass through cache : false as one of the parameters.
For example, straight from the documentation:
$.ajax({
url: "test.php",
cache: false,
success: function(){
//whatever
}
});
Source: http://api.jquery.com/jQuery.ajax/
The best solution I can imagine is to update/redirect to a page that redirects you back to current. I'm not sure if javascript has a ctrl+f5 function.
If using jQuery use the cache option with the .ajax call. Alternatively append a date stamp to the url to force the browser to fetch each time.

Huge Delay on second jQuery Ajax Request

I try to do some simple Ajax call with refresh of page content with this code snippet:
$("a.ajaxify-watched").bind("click", function(event) {
$item = $(this);
$.ajax({
url: $(this).attr("href"),
global: false,
type: "GET",
data: ({
callback : "inline"
}),
dataType: "json",
async:false,
success: function(msg){
if (msg.status == 200) {
toggleStatus($item, msg)
}
}
});
return false;
});
This works perfect and for me theres nothing to worry about the code but the speed it gets executed.
The first time everything works really fine: 47ms for the operation. But after that every other Ajax call get a constant delay of 2.6 seconds - everytime. I checked with Firebug and found that it is shown as "Waiting Time".
I can't really say whats happening here.
We recently switched from pure Apache2 to Nginx Caching Reverse Proxy with load balancing with Apache as Backend Php Interpreter. We could see a huge performance boost and everything is working really fine. I can't tell when my problem first appeared and if it really has something to with our new server setup.
I just found out today, that there is a problem with jQuery so I just wanted to give as much info as possible.
Thank you and let me know if I should provide additional information.
If Firebug indicates waiting it looks like a server issue.
Is there a way to call the pages directly without ajax? If so try to do that and check if the pages load fast or slow?
Also, check that you do not have any events triggering on ajax that might affect this.
Most servers will only handle a single page load from the same server so multiple page loads is handled in queue, so if you have parallell ajax calls they might have to wait for each other.

Categories

Resources