{"id":115,"date":"2014-05-16T00:02:17","date_gmt":"2014-05-15T18:02:17","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=115"},"modified":"2023-10-04T20:44:02","modified_gmt":"2023-10-05T01:44:02","slug":"cors-cross-site-scripting-ie8-ie9","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/cors-cross-site-scripting-ie8-ie9\/","title":{"rendered":"CORS &#8211; Cross Site \/ Domain Scripting &#8211; IE8 &#038; IE9"},"content":{"rendered":"<h3 class=\"wp-block-heading\"><strong>UPDATE 01\/23\/2015<\/strong><\/h3>\n\n\n\n<p>I&#8217;ve realized that CORS can at times be blocked by firewalls and thus have adopted a new personal best practice for performing CORS requests using a server side proxy setup &#8211; which is browser agnostic and thus conditional code is not needed on a per-browser basis. \u00a0I suggest you read <strong><a href=\"https:\/\/promincproductions.com\/blog\/server-proxy-for-cross-origin-resource-sharing-cors\/\" data-lasso-id=\"73\">Successful Cross Site Scripting (CORS) Using A Server Proxy<\/a><\/strong>\u00a0for how to best implement a server side proxy CORS request.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<p>I ran into an annoying problem today&#8230; &nbsp;We were getting complaints that our application wasn&#8217;t working (of course the customers never tell you what browser, what they are doing, etc.). &nbsp;But I knew it was breaking where I to an AJAX call to my NodeJS server, which I&#8217;m calling to via IP address due to our multi-server setup. &nbsp;This technically creates a cross site scripting issue.<\/p>\n\n\n\n<p>I&#8217;m using jQuery to do this AJAX call, and I know I have the jQuery settings for cross-site scripting enabled.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>jQuery.support.cors = true; &nbsp;\/* Allow cross domain scripting *\/<\/code><\/pre>\n\n\n\n<p>So what gives?<\/p>\n\n\n\n<p>Bottom line &#8211; our good &#8216;ole evil friend, Internet Explorer, has a situation where cross site scripting is not supported by our not-so-evil friend jQuery. &nbsp;Now to be fair to IE, this only affects users who have their security settings to block cross site scripting. &nbsp;Which I believe is the default setting&#8230; &nbsp;So we could add a message to the site for them to change their settings. &nbsp;It&#8217;s simple really:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Click on the little gear thingy if in IE9. &nbsp;If your in IE8 use the&nbsp;<strong>settings<\/strong> menu.<\/li>\n\n\n\n<li>Choose&nbsp;<strong>Internet&nbsp;<\/strong><strong>Options<\/strong><\/li>\n\n\n\n<li>Select the&nbsp;<strong>Security<\/strong><strong> tab<\/strong><\/li>\n\n\n\n<li>Click the&nbsp;<strong>Custom Level&#8230;<\/strong> button (why did they add the ellipses there???)<\/li>\n\n\n\n<li>Scroll about 2\/3rds the way down the long list to the&nbsp;<strong>Miscellaneous<\/strong> section<\/li>\n\n\n\n<li>For the option&nbsp;<strong>Access data sources across domains<\/strong>, choose&nbsp;<strong>Enable<\/strong> (or&nbsp;<strong>Prompt<\/strong> if you want the annoying alert box everytime you come across this)<\/li>\n\n\n\n<li>Click&nbsp;<strong>OK<\/strong><\/li>\n\n\n\n<li>Click&nbsp;<strong>OK<\/strong><\/li>\n\n\n\n<li>Reload your page and try again.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image alignnone size-medium wp-image-120\"><img decoding=\"async\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2014\/05\/CORS-500x476.png\" alt=\"CORS - Cross Site \/ Domain AJAX in IE 8 and IE 9\" class=\"wp-image-120\" \/><figcaption class=\"wp-element-caption\">Just ask your users to simply change these settings. Or you could change your code&#8230;.<\/figcaption><\/figure>\n\n\n\n<p>Easy peasy, right?<\/p>\n\n\n\n<p>I guess the other option is to use Javascript \/ jQuery to sniff for only our unfortunate IE friends and do a custom function for just them. &nbsp;Now I should say, IE10 and greater is safe from this, so we don&#8217;t need to treat them special. &nbsp;Oh, and for IE7 and lower this fix does not work &#8211; they are essentially a lost cause in many regards. &nbsp;But IE 8 &amp; IE 9 are fixed with this solution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Solution<\/h2>\n\n\n\n<p>I&#8217;m not going to take credit for the solution &#8211; rather am going to do my best to pass along those with higher knowledge than me. &nbsp;Thank you so much to Tom Elliott at <a href=\"http:\/\/www.webdevdoor.com\/\" target=\"_blank\" rel=\"noopener\" data-lasso-id=\"74\">Web Dev Door<\/a> for clearly documenting the solution I used for this. &nbsp;So the solution is this article:<\/p>\n\n\n\n<p><a href=\"http:\/\/www.webdevdoor.com\/jquery\/cross-domain-browser-json-ajax\/\" target=\"_blank\" rel=\"noopener\" data-lasso-id=\"75\">http:\/\/www.webdevdoor.com\/jquery\/cross-domain-browser-json-ajax\/<\/a><\/p>\n\n\n\n<p>I didn&#8217;t personally need to do anything with the&nbsp;<strong>Step 1<\/strong> portion of his article, but that&#8217;s not to say that your case won&#8217;t require it. &nbsp;This is of course dependent on your server and code setup.<\/p>\n\n\n\n<p>But the&nbsp;<strong>Step 2<\/strong> portion of his article is a brilliant little script that simply detects if you are a IE8 or IE9 visitor. &nbsp;If so, the AJAX request is sent via <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ie\/cc288060%28v=vs.85%29.aspx\" target=\"_blank\" rel=\"noopener\" data-lasso-id=\"76\">xdr &#8211; the Microsoft protocol for cross-domain Asynchronous JavaScript and XML requests<\/a>. &nbsp;If you are NOT on one of those two browsers, the AJAX request is handled via jQuery (or if you have some other means by all means go ahead.<\/p>\n\n\n\n<p>I did do one addition to Tom&#8217;s code. &nbsp;For the IE visitors using xdr, I added an onerror event in the case that something does go wrong. &nbsp;This is something I was doing in my jQuery AJAX call already, so needed to add it here as well.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>xdr.onerror = function() {\n processFinalImageOutputError(xdr.responseText);\n};<\/code><\/pre>\n\n\n\n<p>I created two functions &#8211; one for AJAX success and one for AJAX error and both the xdr and jQuery functions can call those functions in their appropriate instance.<\/p>\n\n\n\n<p>So my final output looks something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var url = 'http:\/\/cross-domain-url.com\/script?param1=something&amp;param2=somethingElse';\nvar browser = navigator.userAgent;\nvar IEversion = 99; \/\/Give a default value for non-IE browsers\n\nif (browser.indexOf(\"MSIE\") &gt; 1) { \/\/Detects if IE\n IEversion = parseInt(browser.substr(browser.indexOf(\"MSIE\")+5, 5));\n}\n\nif (IEversion &lt; 10) {\n \/* For older IE, sending AJAX request using xdr *\/\n xdr = new XDomainRequest(); \/\/ Creates a new XDR object.\n xdr.timeout = 3000;\/\/Set the timeout time to 3 second.\n xdr.onload = function () {\n   successFunction(xdr.responseText);\n };\n xdr.onerror = function () {\n   errorFunction(xdr.responseText);\n };\n xdr.ontimeout = function () {\n   alert(\"Error\");\n };\n \/\/ this also needs to be set\n xdr.onprogress = function() {\n   window.console.log('progress');\n };\n xdr.open(\"GET\", url); \/\/ Creates a cross-domain connection with our target server using GET method.\n xdr.send(); \/\/Send string data to server\n} else {\n \/* For modern browsers, send AJAX request using jQuery *\/\n jQuery.ajax({\n   url: url,\n   dataType: 'json',\n   crossDomain: true,\n   success: function( data ) {\n     successFunction(data);\n   },\n   error: function(xhr, status, errorThrown) {\n     errorFunction(xhr, status, errorThrown);\n   }\n });\n}\n\nfunction successFunction(data) {\n console.log(\"AJAX Request Success\");\n}\n\nfunction errorFunction(xhr, status, errorThrown) {\n console.log(\"AJAX Request ERROR\");\n console.log(xhr);\n console.log(status);\n console.log(errorThrown);\n}\n<\/code><\/pre>\n\n\n\n<p>&nbsp;<strong>UPDATED&nbsp;07\/11\/2014<\/strong><\/p>\n\n\n\n<p>Modified the XDR functions a bit &#8211; according to <a href=\"https:\/\/stackoverflow.com\/users\/54426\/rich-apodaca\" target=\"_blank\" rel=\"noopener\" data-lasso-id=\"77\">Rich Apodaca<\/a> on <a href=\"https:\/\/stackoverflow.com\/questions\/15786966\/xdomainrequest-aborts-post-on-ie-9\" target=\"_blank\" rel=\"noopener\" data-lasso-id=\"78\">this post<\/a>, I was missing a few of the required functions.<\/p>","protected":false},"excerpt":{"rendered":"<p>UPDATE 01\/23\/2015 I&#8217;ve realized that CORS can at times be blocked by firewalls and thus have adopted a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":120,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[12,5],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-jquery","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2014\/05\/CORS.png","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-1R","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":7,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":3477,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions\/3477"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/120"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}