{"id":1376,"date":"2025-01-20T11:16:56","date_gmt":"2025-01-20T05:46:56","guid":{"rendered":"https:\/\/www.msgclub.net\/learn\/?p=1376"},"modified":"2025-01-20T11:16:56","modified_gmt":"2025-01-20T05:46:56","slug":"fixing-cors-policy-issues-step-by-step","status":"publish","type":"post","link":"https:\/\/www.msgclub.net\/learn\/fixing-cors-policy-issues-step-by-step.html","title":{"rendered":"Fixing CORS Policy Issues: Step-by-Step"},"content":{"rendered":"\n<p>CORS errors happen when your browser stops your app from talking to another website or API because it doesn\u2019t trust it yet.&nbsp;<\/p>\n\n\n\n<p>Don\u2019t worry\u2014it\u2019s a common issue, and there are easy ways to fix it. Let\u2019s break it down.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is CORS?<\/h3>\n\n\n\n<p>CORS is a security rule that browsers follow. It checks if your app has permission to ask another website (or API) for data.<\/p>\n\n\n\n<p>If the permission isn\u2019t set up correctly, the browser blocks the request and shows an error like:<\/p>\n\n\n\n<p>\u201cBlocked by CORS policy: No \u2018Access-Control-Allow-Origin\u2019 header is present on the requested resource.\u201d<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Two Common Scenarios for CORS Errors<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>When you call an API from the backend (server-side).<\/li>\n\n\n\n<li>When you call an API directly from the browser (client-side).<\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s fix each case step by step.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Fixing CORS for Server-Side API Calls<\/h3>\n\n\n\n<p>If your app makes requests from the backend (like Node.js or another server), you need to adjust the server\u2019s settings.<\/p>\n\n\n\n<p>How to Fix:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tell the server to allow requests from your app.<\/li>\n\n\n\n<li>This is done by adding your app\u2019s domain or IP address to the server\u2019s &#8220;allowed list.&#8221;<\/li>\n\n\n\n<li>After this, the server will process your requests without any errors.<\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>Imagine your server is a security guard. You give it a list of people (domains or IPs) who are allowed to enter. Once your app is on the list, the guard (server) lets it in!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Fixing CORS for Client-Side API Calls<\/h3>\n\n\n\n<p>When you call an API directly from the browser using JavaScript, the browser might block it because the server didn\u2019t say, \u201cIt\u2019s okay to share data with your app.\u201d<\/p>\n\n\n\n<p>What You\u2019ll See:<strong><br><\/strong>An error message like this:<\/p>\n\n\n\n<p>Access to XMLHttpRequest at &#8216;<a href=\"http:\/\/loginsms.ewyde.com\/rest\/services\/sendSMS\/sendGroupSms?AUTH_KEY=4379ff6baac744178f0876196fccef8&amp;message=Dear+User,+Your+OTP+for+Registration+is+953466.+Sagar+Multispeciality+Hospital,+Bhopal&amp;senderId=SGRHSP&amp;routeId=11&amp;mobileNos=9387306030&amp;smsContentType=English\">http:\/\/yourdomain.com\/rest\/services\/sendSMS\/sendGroupSms?AUTH_KEY=tgdfsrgdfghdf&amp;message=dthisistestsms&amp;senderId=SGRHSP&amp;routeId=11&amp;mobileNos=999999999&amp;smsContentType=English<\/a>&#8216;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Error:<\/h4>\n\n\n\n<p>From origin&#8217;<a href=\"http:\/\/localhost:5173\/\">http:\/\/localhost:5173<\/a>&#8216; has been blocked by CORS policy:&nbsp;&nbsp;<\/p>\n\n\n\n<p>No &#8216;Access-Control-Allow-Origin&#8217; header is present on the requested resource.&nbsp;<\/p>\n\n\n\n<p>How to Fix:<\/p>\n\n\n\n<p>Add an option called mode: &#8216;no-cors&#8217; to your API call. This tells the browser to stop checking the CORS rules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Code<\/h3>\n\n\n\n<p>Here\u2019s a simple example to show how you can fix this issue in your code:<\/p>\n\n\n\n<p><strong>Example of a Client-Side Fix<\/strong><\/p>\n\n\n\n<p>Here\u2019s how you can modify your JavaScript code:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Sample Code:<\/h4>\n\n\n\n<p>document.getElementById(&#8216;sendSmsButton&#8217;).addEventListener(&#8216;click&#8217;, () =&gt; {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;const apiUrl = &#8216;<a href=\"https:\/\/msg.msgclub.net\/rest\/services\/sendSMS\/sendGroupSms?AUTH_KEY=345426&amp;message=Hello&amp;senderId=TBTSIG&amp;routeId=1&amp;mobileNos=9999999999&amp;smsContentType=English\">https:\/\/yourdomain.net\/rest\/services\/sendSMS\/sendGroupSms?AUTH_KEY=dfgdfg&amp;message=Hello&amp;senderId=TBTSIG&amp;routeId=1&amp;mobileNos=9999999999&amp;smsContentType=English<\/a>&#8216;;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;fetch(apiUrl, {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;method: &#8216;GET&#8217;,&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: &#8216;no-cors&#8217;,&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;})&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.then(response =&gt; {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!response.ok) {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new Error(`HTTP error! Status: ${response.status}`);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return response.json();&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.then(data =&gt; {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(&#8216;Response:&#8217;, data);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(&#8216;SMS Sent Successfully!&#8217;);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.catch(error =&gt; {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.error(&#8216;Error:&#8217;, error);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(&#8216;Failed to send SMS!&#8217;);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});&nbsp;&nbsp;<\/p>\n\n\n\n<p>});&nbsp;&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Points to Remember<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <strong>mode: &#8216;no-cors&#8217;<\/strong> for quick fixes in client-side API calls.<\/li>\n\n\n\n<li>Always ensure your <strong>server<\/strong> allows requests from the required domain or IP.<\/li>\n<\/ul>\n\n\n\n<p>By following these simple steps, you can resolve CORS issues and make your API calls work smoothly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CORS errors happen when your browser stops your app from talking to another website or API because it doesn\u2019t trust it yet.&nbsp; Don\u2019t worry\u2014it\u2019s a common issue, and there are easy ways to fix it. Let\u2019s break it down. What is CORS? CORS is a security rule that browsers follow. It checks if your app&hellip; <a class=\"more-link\" href=\"https:\/\/www.msgclub.net\/learn\/fixing-cors-policy-issues-step-by-step.html\">Continue reading <span class=\"screen-reader-text\">Fixing CORS Policy Issues: Step-by-Step<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[187],"tags":[205,196,204,193,191,194,192,198,188,197,190,206,200,199,202,203,195,201,189],"class_list":["post-1376","post","type-post","status-publish","format-standard","hentry","category-cors-policy","tag-api-security","tag-backend-development","tag-browser-security","tag-cors-configuration","tag-cors-error-fix","tag-cors-headers","tag-cors-in-javascript","tag-cors-middleware","tag-cors-policy","tag-cross-origin-requests","tag-cross-origin-resource-sharing","tag-enable-cors","tag-how-cors-works","tag-http-headers","tag-origin-and-headers","tag-rest-api-security","tag-same-origin-policy","tag-web-api-development","tag-web-development","entry"],"_links":{"self":[{"href":"https:\/\/www.msgclub.net\/learn\/wp-json\/wp\/v2\/posts\/1376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.msgclub.net\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.msgclub.net\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.msgclub.net\/learn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.msgclub.net\/learn\/wp-json\/wp\/v2\/comments?post=1376"}],"version-history":[{"count":2,"href":"https:\/\/www.msgclub.net\/learn\/wp-json\/wp\/v2\/posts\/1376\/revisions"}],"predecessor-version":[{"id":1378,"href":"https:\/\/www.msgclub.net\/learn\/wp-json\/wp\/v2\/posts\/1376\/revisions\/1378"}],"wp:attachment":[{"href":"https:\/\/www.msgclub.net\/learn\/wp-json\/wp\/v2\/media?parent=1376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.msgclub.net\/learn\/wp-json\/wp\/v2\/categories?post=1376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.msgclub.net\/learn\/wp-json\/wp\/v2\/tags?post=1376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}