Why do we pass null to XMLHttpRequest.send?

If you’ll take a look at an old specification of XMLHttpRequest, it seems like as though the W3C did not require that the parameter be optional at one point, which may have led to people supplying an explicit null value ‘just in case’. (search for ‘SHOULD support the send’) http://web.archive.org/web/20060409155734/http://www.w3.org/TR/XMLHttpRequest/ Another plausible reason I’ve come … Read more

anti-CSRF token and Javascript

There are several techniques, which when used together provide a sufficient CSRF protection. Unique Token A single, session-specific token is good enough for most applications. Just make sure that your site doesn’t have any XSS vulnerabilities, otherwise any kind of token technique you employ is a waste. AJAX call to regenerate the token is a … Read more

JS/jQuery get HTTPRequest request headers?

If this is for debugging purposes then you can just use Firebug or Chrome Developer Tools (and whatever the feature is called in IE) to examine the network traffic from your browser to the server. An alternative would be to use something like this script: $.ajax({ url: ‘someurl’, headers:{‘foo’:’bar’}, complete: function() { alert(this.headers.foo); } }); … Read more

Loading local files with Javascript without a web server

If you insist on using Chrome, it have some command line flags to allow access to/from local originated files (–allow-file-access-from-files / –disable-web-security). Do note that you need to run entire browser from scratch with those flags – i.e. if there’s already any other Chrome windows open flags WON’T have any effect and that effect persists … Read more

ASP.Net MVC Ajax form with jQuery validation

Try adding an OnBegin callback to the AjaxOptions and return the value of $(‘form’).validate().form() from the callback. Looking at the source it appears that this should work. function ajaxValidate() { return $(‘form’).validate().form(); } <% using (Ajax.BeginForm(“Post”, new AjaxOptions { UpdateTargetId = “GBPostList”, InsertionMode = InsertionMode.InsertBefore, OnBegin = “ajaxValidate”, OnSuccess = “getGbPostSuccess”, OnFailure = “showFaliure” })) … Read more

Chrome’s loading indicator keeps spinning during XMLHttpRequest

I shamelessly stole Oleg’s test case and adjusted it a bit to simulate long-polling. load.html: <!DOCTYPE html> <head> <title>Demonstration of the jQery.load problem</title> <script src=”http://code.jquery.com/jquery-latest.js”></script> <script> jQuery(document).ready(function() { $(‘#main’).load(“test.php”); }); </script> </head> <body> <div id=’main’></div> </body> </html> test.php: <?php sleep(5); ?> <b>OK!</b> The result is interesting: in Firefox and Opera, no loading indicator is shown … Read more

Using $.Deferred() with nested ajax calls in a loop

Yeah, using Deferred is the way to do that: function a() { var def = $.Deferred(); $.ajax(“http://url1”).done(function(data){ var requests = []; for (var i = 0; i < 2; i++) { requests.push(b()); } $.when.apply($, requests).then(function() { def.resolve(); }); }); return def.promise(); } // called by a() function b() { var def = $.Deferred(), requests = … Read more