{"id":22971,"date":"2020-11-17T12:00:05","date_gmt":"2020-11-17T17:00:05","guid":{"rendered":"https:\/\/webdevstudios.com\/?p=22971"},"modified":"2024-04-15T11:56:51","modified_gmt":"2024-04-15T15:56:51","slug":"resolve-cors-errors","status":"publish","type":"post","link":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/","title":{"rendered":"Resolve CORS Errors with WordPress REST API"},"content":{"rendered":"<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Cross-origin_resource_sharing\">Cross-Origin Resource Sharing<\/a> (CORS) is a relatively new problem in JavaScript development. Maybe I&#8217;m showing my age here, but I can distinctly remember when there were no concerns with loading JavaScript from all over the internet into your blog. It was new! It was fun! But as more and more of us got online, and JavaScript\u00a0<em>webpages<\/em> turned into JavaScript\u00a0<em>applications<\/em>, it became a huge security risk. Thus, we have our modern world where a JavaScript file can only be loaded outside of its domain if it contains the correct <code>Access-Control-Allow-Origin<\/code>\u00a0headers.<\/p>\n<p>So, what do you do when you&#8217;re happily making a block (as part of <a href=\"https:\/\/webdevstudios.com\/2020\/09\/08\/gutenberg-first\/\">your agency&#8217;s Gutenberg-first initiative<\/a>, of course), and the API you are trying to access doesn&#8217;t have the correct headers? Let me show you a quick way to resolve a CORS error with the WordPress REST API. In particular, we&#8217;re going to create a custom REST endpoint and proxy our request through the WordPress API.<\/p>\n<p>I ran into this problem when revisiting <a href=\"https:\/\/webdevstudios.com\/2019\/08\/15\/fixing-oembed\/\">my old custom oEmbed provider<\/a>. I wanted to accomplish the same thing, but in JavaScript:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22976\" data-permalink=\"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/screen-shot-2020-11-02-at-2-53-46-pm\/\" data-orig-file=\"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM.png\" data-orig-size=\"1320,210\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screen Shot 2020-11-02 at 2.53.46 PM\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM-1024x163.png\" class=\"alignnone size-large wp-image-22976\" src=\"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM-1024x163.png\" alt=\"Screenshot of code using fetch to call a backend.deviantart.com URL\" width=\"850\" height=\"135\" srcset=\"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM-1024x163.png 1024w, https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM-300x48.png 300w, https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM-768x122.png 768w, https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM-180x29.png 180w, https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM-1300x207.png 1300w, https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM.png 1320w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>However, when trying to load the API from my local machine, I got these errors:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22975\" data-permalink=\"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/screen-shot-2020-11-02-at-2-45-09-pm\/\" data-orig-file=\"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.45.09-PM.png\" data-orig-size=\"1002,460\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screen Shot 2020-11-02 at 2.45.09 PM\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.45.09-PM.png\" class=\"alignnone size-full wp-image-22975\" src=\"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.45.09-PM.png\" alt=\"Access Control Allow Origin errors in the JavaScript console\" width=\"1002\" height=\"460\" srcset=\"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.45.09-PM.png 1002w, https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.45.09-PM-300x138.png 300w, https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.45.09-PM-768x353.png 768w, https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.45.09-PM-131x60.png 131w\" sizes=\"auto, (max-width: 1002px) 100vw, 1002px\" \/><\/p>\n<p>In short, because the remote server <code>backend.deviantart.com<\/code> is not set up to allow an arbitrary web page to load its JavaScript, I could not call the API directly from my block using <code>fetch<\/code>.\u00a0To get around this, we&#8217;ll be using the PHP backend and <code>wp_remote_get<\/code>.<\/p>\n<h3>New REST Endpoint<\/h3>\n<p>First, we need to set up our custom endpoint. This will allow us to call the WordPress backend from our block:<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/oddevan\/788b176a03fa655ba15e672432b78109.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/oddevan\/788b176a03fa655ba15e672432b78109\">Gist<\/a>.<\/noscript><\/div>\n<p>You can <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/register_rest_route\/\">read more about <code>register_rest_route<\/code> in the WordPress docs<\/a>. With this code, we are setting up the following flow:<\/p>\n<ol>\n<li>Block will call <code>\/wp-json\/oddevan\/v1\/devArtProxy\/<\/code><\/li>\n<li>WordPress will call the <code>proxy_deviantart_oembed_security<\/code>\u00a0function to find out if the current user has permissions to access this endpoint<\/li>\n<li>WordPress will call the <code>proxy_deviantart_oembed<\/code>\u00a0function.<\/li>\n<\/ol>\n<p>Next, let&#8217;s check our security function:<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/oddevan\/58c1a6a3b83311e05463ea883fb12719.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/oddevan\/58c1a6a3b83311e05463ea883fb12719\">Gist<\/a>.<\/noscript><\/div>\n<p>As you can see, it&#8217;s not very complicated. We just need to pass along whether the current user can use the editor. If the user can&#8217;t use the editor, there&#8217;s no reason for them to use our endpoint. This will prevent anonymous users (aka, &#8220;anyone on the internet&#8221;) from using our endpoint, possibly for purposes we don&#8217;t like.<\/p>\n<p>Now, let&#8217;s see our proxy function:<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/oddevan\/03c675a843c03b3f66cf1eff012889be.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/oddevan\/03c675a843c03b3f66cf1eff012889be\">Gist<\/a>.<\/noscript><\/div>\n<p>There&#8217;s a bit going on here. First, we get the <code>url<\/code>\u00a0variable from the request and run it through a helper function we wrote to make sure it is actually a DeviantArt URL. Then we use <code>wp_remote_get<\/code>\u00a0to have the PHP backend make the request to DeviantArt&#8217;s oEmbed API. Because requests like this don&#8217;t have the extra considerations that a JavaScript request has, they are not usually subject to CORS. After some quick checking to make sure we have an actual response, we package up the response from DeviantArt inside a <code>WP_REST_Response<\/code>\u00a0object and send it back to our block.<\/p>\n<p>Inside our block, we just have to call <code>apiFetch<\/code>\u00a0to have WordPress automatically set up the request to our endpoint:<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/oddevan\/f99f31381e2d4c46261908f9058a5788.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/oddevan\/f99f31381e2d4c46261908f9058a5788\">Gist<\/a>.<\/noscript><\/div>\n<p>Because <code>apiFetch<\/code>\u00a0returns a <code>Promise<\/code>, we need to use <code>async<\/code>\u00a0and <code>await<\/code>\u00a0when using it. But that&#8217;s another blog post.<\/p>\n<p><a href=\"https:\/\/github.com\/oddevan\/oddevan-deviantart-embed\">You can see the whole thing in action in the finished plugin.<\/a>\u00a0It&#8217;s built using our own <a href=\"https:\/\/webdevstudios.com\/2020\/06\/16\/wordpress-block-development\/\">WDS Block Starter<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cross-Origin Resource Sharing (CORS) is a relatively new problem in JavaScript development. Maybe I&#8217;m showing my age here, but I can distinctly remember when there were no concerns with loading JavaScript from all over the internet into your blog. It was new! It was fun! But as more and more of us got online, and <a class=\"more-link\" href=\"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/\">Read More<span class=\"screen-reader-text\"> Resolve CORS Errors with WordPress REST API<\/span><\/a><\/p>\n","protected":false},"author":106,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"footnotes":""},"categories":[142],"tags":[13189,801],"coauthors":[9212],"class_list":["post-22971","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-cors","tag-wordpress-rest-api"],"acf":{"blog_hero_image":{"ID":23017,"id":23017,"title":"CORS Error","filename":"CORS-Error.jpg","filesize":75121,"url":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error.jpg","link":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/cors-error\/","alt":"","author":"6","description":"","caption":"","name":"cors-error","status":"inherit","uploaded_to":22971,"date":"2020-11-16 17:48:50","modified":"2020-11-16 17:48:50","menu_order":0,"mime_type":"image\/jpeg","type":"image","subtype":"jpeg","icon":"https:\/\/webdevstudios.com\/wp-includes\/images\/media\/default.png","width":1920,"height":720,"sizes":{"thumbnail":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-150x150.jpg","thumbnail-width":150,"thumbnail-height":150,"medium":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-300x113.jpg","medium-width":300,"medium-height":113,"medium_large":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-768x288.jpg","medium_large-width":768,"medium_large-height":288,"large":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-1024x384.jpg","large-width":850,"large-height":319,"1536x1536":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-1536x576.jpg","1536x1536-width":1536,"1536x1536-height":576,"2048x2048":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error.jpg","2048x2048-width":1920,"2048x2048-height":720,"featured-work-lg":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-436x511.jpg","featured-work-lg-width":436,"featured-work-lg-height":511,"featured-work-sm":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-436x241.jpg","featured-work-sm-width":436,"featured-work-sm-height":241,"book-cover":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-235x300.jpg","book-cover-width":235,"book-cover-height":300,"home-hero":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-1350x440.jpg","home-hero-width":1350,"home-hero-height":440,"services-screenshot":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-590x720.jpg","services-screenshot-width":590,"services-screenshot-height":720,"single-blog-featured":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-1920x625.jpg","single-blog-featured-width":1920,"single-blog-featured-height":625,"single-blog-inline":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-850x360.jpg","single-blog-inline-width":850,"single-blog-inline-height":360,"grid-image":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-420x420.jpg","grid-image-width":420,"grid-image-height":420,"logo-train":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-160x60.jpg","logo-train-width":160,"logo-train-height":60,"simple-header":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-1920x191.jpg","simple-header-width":1920,"simple-header-height":191,"full-width":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error.jpg","full-width-width":1920,"full-width-height":720,"fifty-fifty-media":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error-1300x488.jpg","fifty-fifty-media-width":1300,"fifty-fifty-media-height":488,"gform-image-choice-sm":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error.jpg","gform-image-choice-sm-width":300,"gform-image-choice-sm-height":113,"gform-image-choice-md":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error.jpg","gform-image-choice-md-width":400,"gform-image-choice-md-height":150,"gform-image-choice-lg":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/CORS-Error.jpg","gform-image-choice-lg-width":600,"gform-image-choice-lg-height":225}}},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.5 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Resolve CORS Errors with WordPress REST API - WebDevStudios<\/title>\n<meta name=\"description\" content=\"Let Backend Engineer, Evan Hildreth, show you a quick way to resolve a CORS error with the WordPress REST API.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Resolve CORS Errors with WordPress REST API\" \/>\n<meta property=\"og:description\" content=\"Let Backend Engineer, Evan Hildreth, show you a quick way to resolve a CORS error with the WordPress REST API.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/\" \/>\n<meta property=\"og:site_name\" content=\"WebDevStudios\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/webdevstudios\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-17T17:00:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T15:56:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/Social-Template-22.png\" \/>\n\t<meta property=\"og:image:width\" content=\"560\" \/>\n\t<meta property=\"og:image:height\" content=\"315\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Evan Hildreth\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@oddevan\" \/>\n<meta name=\"twitter:site\" content=\"@webdevstudios\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Evan Hildreth\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/\"},\"author\":{\"name\":\"Evan Hildreth\",\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/#\\\/schema\\\/person\\\/334b3d74f3dabf15d94e275bd09f21cd\"},\"headline\":\"Resolve CORS Errors with WordPress REST API\",\"datePublished\":\"2020-11-17T17:00:05+00:00\",\"dateModified\":\"2024-04-15T15:56:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/\"},\"wordCount\":601,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/webdevstudios.com\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/Screen-Shot-2020-11-02-at-2.53.46-PM-1024x163.png\",\"keywords\":[\"CORS\",\"WordPress REST API\"],\"articleSection\":[\"WordPress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/\",\"url\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/\",\"name\":\"Resolve CORS Errors with WordPress REST API - WebDevStudios\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/webdevstudios.com\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/Screen-Shot-2020-11-02-at-2.53.46-PM-1024x163.png\",\"datePublished\":\"2020-11-17T17:00:05+00:00\",\"dateModified\":\"2024-04-15T15:56:51+00:00\",\"description\":\"Let Backend Engineer, Evan Hildreth, show you a quick way to resolve a CORS error with the WordPress REST API.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/#primaryimage\",\"url\":\"https:\\\/\\\/webdevstudios.com\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/Screen-Shot-2020-11-02-at-2.53.46-PM.png\",\"contentUrl\":\"https:\\\/\\\/webdevstudios.com\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/Screen-Shot-2020-11-02-at-2.53.46-PM.png\",\"width\":1320,\"height\":210,\"caption\":\"Screenshot of code using fetch to call a backend.deviantart.com URL\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/2020\\\/11\\\/17\\\/resolve-cors-errors\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/webdevstudios.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resolve CORS Errors with WordPress REST API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/#website\",\"url\":\"https:\\\/\\\/webdevstudios.com\\\/\",\"name\":\"WebDevStudios\",\"description\":\"WordPress Design and Development Agency\",\"publisher\":{\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/webdevstudios.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/#organization\",\"name\":\"WebDevStudios\",\"url\":\"https:\\\/\\\/webdevstudios.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/webdevstudios.com\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/wds-amp-logo.png\",\"contentUrl\":\"https:\\\/\\\/webdevstudios.com\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/wds-amp-logo.png\",\"width\":173,\"height\":60,\"caption\":\"WebDevStudios\"},\"image\":{\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"http:\\\/\\\/facebook.com\\\/webdevstudios\",\"https:\\\/\\\/x.com\\\/webdevstudios\",\"http:\\\/\\\/instagram.com\\\/webdevstudios\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/webdevstudios-llc-\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/webdevstudios.com\\\/#\\\/schema\\\/person\\\/334b3d74f3dabf15d94e275bd09f21cd\",\"name\":\"Evan Hildreth\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/14fc029ad1fb712dacf87e5f94e2e53a16d6846191a5913251429a18618c6a90?s=96&d=mm&r=g0d80f659b7503beb5312e4332f5c103a\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/14fc029ad1fb712dacf87e5f94e2e53a16d6846191a5913251429a18618c6a90?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/14fc029ad1fb712dacf87e5f94e2e53a16d6846191a5913251429a18618c6a90?s=96&d=mm&r=g\",\"caption\":\"Evan Hildreth\"},\"sameAs\":[\"http:\\\/\\\/eph.me\\\/\",\"https:\\\/\\\/linkedin.com\\\/in\\\/evanhildreth\",\"https:\\\/\\\/x.com\\\/oddevan\"],\"url\":\"https:\\\/\\\/webdevstudios.com\\\/author\\\/evan-hildrethwebdevstudios-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Resolve CORS Errors with WordPress REST API - WebDevStudios","description":"Let Backend Engineer, Evan Hildreth, show you a quick way to resolve a CORS error with the WordPress REST API.","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:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/","og_locale":"en_US","og_type":"article","og_title":"Resolve CORS Errors with WordPress REST API","og_description":"Let Backend Engineer, Evan Hildreth, show you a quick way to resolve a CORS error with the WordPress REST API.","og_url":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/","og_site_name":"WebDevStudios","article_publisher":"http:\/\/facebook.com\/webdevstudios","article_published_time":"2020-11-17T17:00:05+00:00","article_modified_time":"2024-04-15T15:56:51+00:00","og_image":[{"width":560,"height":315,"url":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2022\/11\/Social-Template-22.png","type":"image\/png"}],"author":"Evan Hildreth","twitter_card":"summary_large_image","twitter_creator":"@oddevan","twitter_site":"@webdevstudios","twitter_misc":{"Written by":"Evan Hildreth","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/#article","isPartOf":{"@id":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/"},"author":{"name":"Evan Hildreth","@id":"https:\/\/webdevstudios.com\/#\/schema\/person\/334b3d74f3dabf15d94e275bd09f21cd"},"headline":"Resolve CORS Errors with WordPress REST API","datePublished":"2020-11-17T17:00:05+00:00","dateModified":"2024-04-15T15:56:51+00:00","mainEntityOfPage":{"@id":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/"},"wordCount":601,"commentCount":0,"publisher":{"@id":"https:\/\/webdevstudios.com\/#organization"},"image":{"@id":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/#primaryimage"},"thumbnailUrl":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM-1024x163.png","keywords":["CORS","WordPress REST API"],"articleSection":["WordPress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/","url":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/","name":"Resolve CORS Errors with WordPress REST API - WebDevStudios","isPartOf":{"@id":"https:\/\/webdevstudios.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/#primaryimage"},"image":{"@id":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/#primaryimage"},"thumbnailUrl":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM-1024x163.png","datePublished":"2020-11-17T17:00:05+00:00","dateModified":"2024-04-15T15:56:51+00:00","description":"Let Backend Engineer, Evan Hildreth, show you a quick way to resolve a CORS error with the WordPress REST API.","breadcrumb":{"@id":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/#primaryimage","url":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM.png","contentUrl":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-02-at-2.53.46-PM.png","width":1320,"height":210,"caption":"Screenshot of code using fetch to call a backend.deviantart.com URL"},{"@type":"BreadcrumbList","@id":"https:\/\/webdevstudios.com\/2020\/11\/17\/resolve-cors-errors\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webdevstudios.com\/"},{"@type":"ListItem","position":2,"name":"Resolve CORS Errors with WordPress REST API"}]},{"@type":"WebSite","@id":"https:\/\/webdevstudios.com\/#website","url":"https:\/\/webdevstudios.com\/","name":"WebDevStudios","description":"WordPress Design and Development Agency","publisher":{"@id":"https:\/\/webdevstudios.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webdevstudios.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webdevstudios.com\/#organization","name":"WebDevStudios","url":"https:\/\/webdevstudios.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webdevstudios.com\/#\/schema\/logo\/image\/","url":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2019\/07\/wds-amp-logo.png","contentUrl":"https:\/\/webdevstudios.com\/wp-content\/uploads\/2019\/07\/wds-amp-logo.png","width":173,"height":60,"caption":"WebDevStudios"},"image":{"@id":"https:\/\/webdevstudios.com\/#\/schema\/logo\/image\/"},"sameAs":["http:\/\/facebook.com\/webdevstudios","https:\/\/x.com\/webdevstudios","http:\/\/instagram.com\/webdevstudios","https:\/\/www.linkedin.com\/company\/webdevstudios-llc-\/"]},{"@type":"Person","@id":"https:\/\/webdevstudios.com\/#\/schema\/person\/334b3d74f3dabf15d94e275bd09f21cd","name":"Evan Hildreth","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/14fc029ad1fb712dacf87e5f94e2e53a16d6846191a5913251429a18618c6a90?s=96&d=mm&r=g0d80f659b7503beb5312e4332f5c103a","url":"https:\/\/secure.gravatar.com\/avatar\/14fc029ad1fb712dacf87e5f94e2e53a16d6846191a5913251429a18618c6a90?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/14fc029ad1fb712dacf87e5f94e2e53a16d6846191a5913251429a18618c6a90?s=96&d=mm&r=g","caption":"Evan Hildreth"},"sameAs":["http:\/\/eph.me\/","https:\/\/linkedin.com\/in\/evanhildreth","https:\/\/x.com\/oddevan"],"url":"https:\/\/webdevstudios.com\/author\/evan-hildrethwebdevstudios-com\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3WX6u-5Yv","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webdevstudios.com\/wp-json\/wp\/v2\/posts\/22971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webdevstudios.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webdevstudios.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webdevstudios.com\/wp-json\/wp\/v2\/users\/106"}],"replies":[{"embeddable":true,"href":"https:\/\/webdevstudios.com\/wp-json\/wp\/v2\/comments?post=22971"}],"version-history":[{"count":0,"href":"https:\/\/webdevstudios.com\/wp-json\/wp\/v2\/posts\/22971\/revisions"}],"wp:attachment":[{"href":"https:\/\/webdevstudios.com\/wp-json\/wp\/v2\/media?parent=22971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webdevstudios.com\/wp-json\/wp\/v2\/categories?post=22971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webdevstudios.com\/wp-json\/wp\/v2\/tags?post=22971"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/webdevstudios.com\/wp-json\/wp\/v2\/coauthors?post=22971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}