Steps to activate CORS using .htaccess in a ZURB Foundation Project

Embarking on my first website development project from scratch, I took on the task of handling front-end, back-end, and database integration. Utilizing the ZURB Foundation Framework (ZURB Template), I structured my project to be modular with Webpack4, Babel7, and Gulp as the Taskrunner.

Downloading the latest XAMPP distribution for Windows, I set up Apache to run locally on port 8099. My backend successfully interacts with the front-end, with the root directory pointing to

D:\foundationtests\src\assets\php
where my PHP files are located.

E:\xampp\apache\bin>httpd -M
headers_module (shared)

Having ensured that the headers_module in Apache is active, my jQuery AJAX calls the backend like this:

function phpAJAX(){
  console.log("phpAJAX was called")
  $.get("http://localhost:8099/test.php", {

  }).then((response) => {
    console.log(response)
  })
}

The PHP file being called simply echoes "Hello!". When I enable the CORS header, everything works smoothly, but to improve this setup, I introduced an .htaccess file based on recommendations from the ZURB Foundation forum.

Despite trying various configurations suggested on forums, including Stack Overflow, the CORS issues persist, leading me to question if my .htaccess file is being recognized at all.

Viewing the network monitor in the browser console for the request provides further insight into the issue:https://i.stack.imgur.com/tGUAi.jpg

Answer №1

To activate CORS, insert the following code into your .htaccess file. By doing this, you will guarantee that these headers are consistently applied:

Header add Access-Control-Allow-Origin: "*"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header add Access-Control-Allow-Headers: "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control"

No matter how you construct your project, it is crucial to have the .htaccess file situated at the root of the directory with the aforementioned content included.

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

Is there a way to refresh the index data with ajax jQuery?

I'm working with an array of data retrieved via the Ajax GET method. Currently, I am displaying this data using its index number such as data[0]. To access the next set of data, I click on a button that should update the index from "here 0" to fetch d ...

Trouble arises with AJAX due to DOM traversal errors

I am trying to set up a system for liking and disliking with a counter. However, I am facing issues with my AJAX call, specifically when attempting to change the HTML of selected elements in the view after sending values to the DB. The element in question ...

The jQuery ajax request was unsuccessful in connecting to the remote server

I've tried researching and troubleshooting, but I still can't figure out why the Ajax code is not functioning correctly. Here is my JavaScript code: $(document).ready(function(){ $("#tform").submit(function() { var varUserName ...

The admin-ajax.php file in Wordpress is sending back php code instead of the expected json format

How do I configure WordPress to return JSON on Nginx? When making Ajax calls in my WordPress site, instead of receiving plain JSON, I see <?php JSON. Everything works fine on the site, except for admin-ajax calls. Example of a regular call: https://i. ...

Tips for sending two objects simultaneously to a template through a Rails Ajax request

Looking to pass two objects to a template that will be rendered after an Ajax call using Rails 3. Take a look at my controller file below. controller/homes_controller.rb class HomesController < ApplicationController def home @sdf=TSdf.n ...

Is there a JavaScript function available that can be used to execute a script once content has been loaded via AJAX?

There are multiple inquiries regarding executing a JavaScript function after content is loaded via Ajax. I am familiar with the common solution of running JS after Ajax load completion. If more than 40 pages are loading a page my-Page.php using Ajax, is t ...

Organizing data with Tablesorter and preserving the sorting order

My table contains valuable information that is initially generated from a PHP script and then updated every n-seconds by checking the database. To enhance the functionality of my table, I decided to install the tablesorter plugin. However, I encountered a ...

Looking to arrange an array group based on a particular key value in PHP?

I require assistance with organizing an array that contains different groups with the same value for each group. I am looking to sort the groups in descending order. Please see the example below: Array ( [0] => Array ( [0] => ...

What is the best approach for managing and obtaining accurate JSON responses when working with PHP API and AngularJS 2 services?

Encountering a backend issue with MySQL, wherein one query is producing a specific dataset: {"candidat":[{"ID":1,"nom":"Danny","prenom":"Hariot","parti":"Quamba","departement":"Ukraine","commune":"Chapayeve"},{"ID":2,"nom":"Shari","prenom":"Adamkiewicz"," ...

Challenge encountered in Ajax with data processing

Help needed with making an ajax request to update the "status" parameter of my "task" object. The script is set up to retrieve the task ID and the new status, but I'm encountering errors in either the routes.rb file, the update_status function within ...

The variable is unable to be accessed within the PHP function query

After passing a variable through ajax to a function within my php file "Cart_code.php", I encountered an issue where the variable was not accessible inside the function. Can you help me figure out why? Javascript $.ajax({ type: "POST", url: "incl ...

The properties of a JSON object are not explicitly defined

When I make an AJAX call, I receive a JSON object and log it using this code: console.log(response); This is the response that gets logged in the console: {"filename":"new.jpg","orientation":"vertical"} However, when I try to access the orientation pro ...

Preloading images before loading a div using JavaScript

Can you walk me through implementing object first and then mergeObject in JavaScript? I have an interesting scenario where I need to display the original list followed by a merged list after a short delay. How can I achieve this using JavaScript? Specific ...

PHP: Checking for IP addresses in a text file

I'm working on a way to capture the IP address of a user and save it in a text file when they pay to access specific content. Once the IP is saved in the logFile.txt, I want to redirect the paying user to an exclusive content page. Additionally, I&apo ...

Having trouble sending a parameter to the Controller in Visual Studio (version 2019)?

I'm facing an issue with passing parameters from a method to the Controller. Interestingly, when I manually run it with the parameter in Postman, everything works fine. Is there a way to pass the parameter to the Controller without creating individua ...

Executing CMD commands in PHP using exec or system functions

I attempted to execute cmd commands and system functions using PHP code, but I am not receiving any output. Should I adjust settings in the php.ini file to enable this feature? ...

Struggling to create a basic CMS using PHP and MySQL, unable to successfully display the data

My latest project involves creating a basic CMS for a website. This is my first time working with MySQL and one of my early experiences with PHP, so I see it as a learning opportunity. Despite consulting numerous resources, the code I've written only ...

Do not retrieve emails from the database if the cell is empty

I created a code that retrieves emails from all users and saves them in a text file. However, there are some users who don't have an email because they registered using their phone number. When I try to fetch the emails, it gets dumped into the text f ...

The Codeigniter validation feature seems to be malfunctioning as it is causing the page to direct to a blank

Hello, I'm new to CodeIgniter and I have a question regarding the form validation in my view page. I have tried adding some code to activate the form_validator for the view but it seems to be redirecting the page to a blank page. Here is the code I a ...

The value of the variable increases, but when I pass it to a function, it does not behave as anticipated

I am experiencing a small issue with some code that I am using to determine if a browser is IE6 or IE7. I suspect that the problem lies in the $browserCheck variable. My initial goal was to have the $browserCheck variable increment, but for testing purpo ...