The CORS policy restrictions are preventing access from the origin. An error occurred while attempting to send the email

I'm running into a problem with sending emails from my website that was developed using HTML, CSS, and JavaScript.

Whenever I try to send an email, I receive the following error message:

Access to XMLHttpRequest at '' from origin '' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

If anyone could provide assistance in resolving this issue, I would greatly appreciate it.

Answer №1

Make sure to include the following line at the top of every page or in a common top PHP file.

<?php
//Session start section
?>    
<?php

//Don't forget to insert these lines
$origin = $_SERVER['HTTP_ORIGIN'];
// PHP 7.x version array
$allowed_domains = ['http://mobilfiksern.no','http://www.mobilfiksern.no'];
// PHP 5.x version array
//$allowed_domains = array("http://mobilfiksern.no", "http://www.mobilfiksern.no");

if (in_array($origin, $allowed_domains)) {
    header('Access-Control-Allow-Origin: ' . $origin);
}
?>
<?php
//Standard codes go here
?>
<?php //Begin HTML tags ?>
<html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Master the art of returning two functions within a single function in Javascript with NodeJS and ExpressJS

Currently, I am facing an issue where I need to combine two objects and return them in one function. The challenge lies in the fact that both objects have almost identical properties, but different values. To tackle this problem, I created two separate fu ...

I'm encountering an issue with my Angular CLI project where it's showing an error message that says "Module cannot be found."

Currently, I am diving into an Angular-cli tutorial. In one of my component TS files, there seems to be a problem with importing a class from another directory as it is not being recognized. Below is the content of my component file (display-user-data-for ...

Having trouble with jQuery's .each method not looping through the list items

Have several li's containing true or false questions. Issue is that it is functioning properly for the first li, but not for the subsequent ones. Jquery code: <script> jQuery(document).ready(function() { //Run the each it ...

React JS currently throws an error stating that the variable 'state' is not recognized and is therefore undefined (no-undef

As I dive into learning ReactJS, I encountered an error message stating that 'state' is not defined (no-undef). I am currently using React version "react": "^16.8.6". When attempting to address this by adding this.state, I was met with the follow ...

Is there a way to make a jQuery dialog function similarly to a JavaScript alert box?

The topic of replacing the standard 'alert' with a customized jQuery dialog has been discussed before, as seen in this thread: Custom alert and confirm box in jquery In that conversation, Hemant Malpote pointed out that: "But it [the suggested ...

Do elements containing {visibility:hidden} properties exist within the HTML DOM structure?

Do HTML elements that have the css property visibility:hidden still exist within the DOM tree? ...

Arranging JSON objects in an array based on date, with the ability to include a 13th month in the date format

I have a function that successfully sorts an array of JSON objects by date, but I need it to also accommodate the 13th month (salary). module = {}; module.exports = [ { "date": "01-2012" }, { "date": "12-2011" }, { ...

Access-Control-Allow-Origin does not permit this origin

function getTotalResultsInfo(gAuthor, gOther) { // Create the appropriate http request var url_to_get = "http://scholar.google.com/scholar?as_q=" + gOther + "&num=" + ret_results + "&as_sauthors=" + gAuthor; $.getJSON(url_to_get, function (data) { ...

Displaying an image from your computer

For my class project, I am creating a simulated forum where users can change their profile picture in the "your profile" tab. Since this is not a real forum and does not involve servers or actual uploading, I am steering clear of using PHP. Instead, I want ...

Flattening an array of Map in Typescript involves combining all the

I am working with an array containing entries of type Map<string, number> Is there a way to flatten this array into a single map? Map<string, number>[] converted to Map<string, number> Appreciate any help on this matter. ...

In Three.js r74, JSONLoader mistakenly links a duplicate of all geometry to the initial bone

I have gained more insight into the bug, so I am rewriting this question. It seems that when using the JSONLoader in r74, the first named bone in an exported Maya scene ends up with a duplicate of all the geometry. EDIT: Check out this JSFiddle for refere ...

Populate a table with data from a list using C# and asp.net

I am currently working with a class called Car, which has attributes like Model and Year. I also have a list of objects of type Car, stored in List<Car> carList = new List<Car>(). My goal is to display this list within an HTML tag, but I am uns ...

Is it possible to launch an Electron application by clicking a run button in the VSCode

I'm new to using VSCode and am currently working on an HTML5 app with Electron. I'm finding it cumbersome to switch between windows and enter a command each time I want to test my application. Is there a way to configure VSCode to run my Electron ...

Leveraging promises for Axios API calls

I'm currently figuring out the most efficient way to accomplish a task. When I visit a Profile page, the Profile component loads the data for that profile and assigns it to this.profile. Within this data is a file path where I intend to process some d ...

What could be the reason my Angular interceptor isn't minified correctly?

I have come across this interceptor in my Angular project: angular.module('dwExceptionHandler', []) .factory('ExceptionInterceptor', ['$q', function ($q) { return function (promise) { return promise.th ...

What is the best way to arrange the elements in this JavaScript array?

Is there a way to arrange this array in order? [ {id : 1, start : 60, end : 120}, {id : 2, start : 100, end : 240}, {id : 3, start : 700, end : 720} ] LATEST UPDATE: Given the current format of my array, is it possible to sort it by the start value ...

Angular 2 experiencing issues with the authorization header

Hello there! I am currently working with the Ionic 2 framework alongside Angular, and I'm facing an issue while trying to make an HTTP request with the authorization header. It seems like the header is not being sent properly. Can someone help me iden ...

Experimenting with an Angular Controller that invokes a service and receives a promise as a

I am currently in the process of testing an angular controller that relies on a service with a method that returns a promise. I have created a jasmine spy object to simulate the service and its promise-returning method. However, my mock promise is not retu ...

"Implementing legends in charts with c3.js and JSON data: A step-by-step guide

Utilizing the C3 chart library to showcase data in my project, I aim to display legends (labels) instead of numbers on the AxisX: Data JSON format: [ {"Label":"DQUA","Aut":3.75,"NoAut":3.75,"CM":32}, {"Label":"DPRO","Aut":43.9,"NoAut":0,"CM":144} ...

Is there a way to integrate nested HTTP requests to refresh my dictionary?

Explaining this may be a bit confusing, so please bear with me as I try to make it clear. Currently, in my project, I am utilizing Node.js along with the Axios and Cheerio modules. The main goal is to fetch HTML data from an e-commerce website (similar t ...