{"id":1779,"date":"2021-04-10T08:26:23","date_gmt":"2021-04-10T08:26:23","guid":{"rendered":"https:\/\/gauravw.com\/blog\/?p=1779"},"modified":"2021-04-10T08:26:25","modified_gmt":"2021-04-10T08:26:25","slug":"cors-cross-origin-resource-sharing","status":"publish","type":"post","link":"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/","title":{"rendered":"CORS &#8211; Cross Origin Resource Sharing"},"content":{"rendered":"<p class=\"wp-block-paragraph\">The browser has a CORS mechanism which\u00a0 restricts the loading of resources from another domain. CORS defines a way in which a browser and server can interact to determine whether it is safe to allow the cross-origin request<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The header which controls accesses is<br>Access-Control-Allow-Origin <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So if this header is not present, the CORS request fails. Even if it is present, it should be having the value which allows the requesting domain in it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To allow all domains one can use \u201c*\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, one web page can freely embed images, videos, stylesheets etc. But AJAX requests to another domain aren&#8217;t allowed as per this rule.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Preflight Request<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Most modern browsers make an extra request prior to executing the requested AJAX request to the other server asking them if such a request is allowed.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The requested server needs to be respond with two headers<br>If service.example.com is willing to accept the action, it may respond with the following headers:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Access-Control-Allow-Origin: http:\/\/www.example.com<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Access-Control-Allow-Methods: PUT, DELETE<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the origin and method both are allowed as mentioned in the request, it then goes ahead and makes the request else leaves it.<\/p>\n\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1779\" data-postid=\"1779\" class=\"themify_builder_content themify_builder_content-1779 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->","protected":false},"excerpt":{"rendered":"<p>The browser has a CORS mechanism which\u00a0 restricts the loading of resources from another domain. CORS defines a way in which a browser and server can interact to determine whether it is safe to allow the cross-origin request The header which controls accesses isAccess-Control-Allow-Origin So if this header is not present, the CORS request fails. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[97],"tags":[],"class_list":["post-1779","post","type-post","status-publish","format-standard","hentry","category-tech-learnings","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>CORS - Cross Origin Resource Sharing &#187; Gaurav Wadhwani<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CORS - Cross Origin Resource Sharing &#187; Gaurav Wadhwani\" \/>\n<meta property=\"og:description\" content=\"The browser has a CORS mechanism which\u00a0 restricts the loading of resources from another domain. CORS defines a way in which a browser and server can interact to determine whether it is safe to allow the cross-origin request The header which controls accesses isAccess-Control-Allow-Origin So if this header is not present, the CORS request fails. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/\" \/>\n<meta property=\"og:site_name\" content=\"Gaurav Wadhwani\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-10T08:26:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-10T08:26:25+00:00\" \/>\n<meta name=\"author\" content=\"Gaurav Wadhwani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gaurav Wadhwani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/cors-cross-origin-resource-sharing\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/cors-cross-origin-resource-sharing\\\/\"},\"author\":{\"name\":\"Gaurav Wadhwani\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"headline\":\"CORS &#8211; Cross Origin Resource Sharing\",\"datePublished\":\"2021-04-10T08:26:23+00:00\",\"dateModified\":\"2021-04-10T08:26:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/cors-cross-origin-resource-sharing\\\/\"},\"wordCount\":196,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"articleSection\":[\"Tech Learnings\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/cors-cross-origin-resource-sharing\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/cors-cross-origin-resource-sharing\\\/\",\"url\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/cors-cross-origin-resource-sharing\\\/\",\"name\":\"CORS - Cross Origin Resource Sharing &#187; Gaurav Wadhwani\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#website\"},\"datePublished\":\"2021-04-10T08:26:23+00:00\",\"dateModified\":\"2021-04-10T08:26:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/cors-cross-origin-resource-sharing\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/cors-cross-origin-resource-sharing\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/cors-cross-origin-resource-sharing\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CORS &#8211; Cross Origin Resource Sharing\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/\",\"name\":\"Gaurav Wadhwani\",\"description\":\"Where I write \\\/ scribble\",\"publisher\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\",\"name\":\"Gaurav Wadhwani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"caption\":\"Gaurav Wadhwani\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\"},\"sameAs\":[\"http:\\\/\\\/gauravw.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"CORS - Cross Origin Resource Sharing &#187; Gaurav Wadhwani","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/","og_locale":"en_US","og_type":"article","og_title":"CORS - Cross Origin Resource Sharing &#187; Gaurav Wadhwani","og_description":"The browser has a CORS mechanism which\u00a0 restricts the loading of resources from another domain. CORS defines a way in which a browser and server can interact to determine whether it is safe to allow the cross-origin request The header which controls accesses isAccess-Control-Allow-Origin So if this header is not present, the CORS request fails. [&hellip;]","og_url":"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/","og_site_name":"Gaurav Wadhwani","article_published_time":"2021-04-10T08:26:23+00:00","article_modified_time":"2021-04-10T08:26:25+00:00","author":"Gaurav Wadhwani","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Gaurav Wadhwani","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/#article","isPartOf":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/"},"author":{"name":"Gaurav Wadhwani","@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"headline":"CORS &#8211; Cross Origin Resource Sharing","datePublished":"2021-04-10T08:26:23+00:00","dateModified":"2021-04-10T08:26:25+00:00","mainEntityOfPage":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/"},"wordCount":196,"commentCount":0,"publisher":{"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"articleSection":["Tech Learnings"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/","url":"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/","name":"CORS - Cross Origin Resource Sharing &#187; Gaurav Wadhwani","isPartOf":{"@id":"https:\/\/gauravw.com\/blog\/#website"},"datePublished":"2021-04-10T08:26:23+00:00","dateModified":"2021-04-10T08:26:25+00:00","breadcrumb":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/cors-cross-origin-resource-sharing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gauravw.com\/blog\/"},{"@type":"ListItem","position":2,"name":"CORS &#8211; Cross Origin Resource Sharing"}]},{"@type":"WebSite","@id":"https:\/\/gauravw.com\/blog\/#website","url":"https:\/\/gauravw.com\/blog\/","name":"Gaurav Wadhwani","description":"Where I write \/ scribble","publisher":{"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gauravw.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e","name":"Gaurav Wadhwani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","caption":"Gaurav Wadhwani"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g"},"sameAs":["http:\/\/gauravw.com"]}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"builder_content":"","_links":{"self":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/comments?post=1779"}],"version-history":[{"count":1,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1779\/revisions"}],"predecessor-version":[{"id":1780,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1779\/revisions\/1780"}],"wp:attachment":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/media?parent=1779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/categories?post=1779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/tags?post=1779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}