The issue of missing the 'Access-Control-Allow-Origin' header on the requested resource cannot be rectified using Php header

I am trying to make an Ajax request for a URL like this:

$.ajax({url: "http://techparty.ir/e.php", success: function(result){
        $("#div1").html(result);
    }});

However, I encountered the following error:

XMLHttpRequest cannot load http://techparty.ir/e.php?_=1437826051819. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://domain.com' is therefore not allowed access.

I have searched for solutions to this problem but couldn't find any helpful information. I came across these links: No 'Access-Control-Allow-Origin' header is present on the requested resource error

Some recommendations suggest placing this code at the top of the PHP file:

header("Access-Control-Allow-Origin: *");   

However, this solution did not work for me. Here is the source code (e.php) that includes the headers:

<?php
    header("Access-Control-Allow-Origin: *");   
    header("Access-Control-Allow-Credentials: true ");
    header("Access-Control-Allow-Methods: OPTIONS, GET, POST");
    header("Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, 
        X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control");
    echo "ehsan";
    ?>

Can you help me solve this problem?

Answer №1

To allow cross-origin resource sharing, you can include the following code in your .htaccess file:

<IfModule mod_headers.c>
   Header set Access-Control-Allow-Origin "*"
 </IfModule>

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

PHP - Sending a JSON response from a class

I am working with a class: class A { protected $name; public function getName() { return $this->name . " example"; } public function setName($name) { $this->name = $name; } } When I run the following code: $r ...

displaying the worth of my models on a Codeigniter view

Recently started working with codeigniter and encountering some issues with my function. Below is the code snippet from my model: public function kode_unik(){ $q = $this->db->query("select MAX(RIGHT(id_obat,5)) as code_max from obat"); $cod ...

Django views are not receiving multiselect data from Ajax requests

As a newcomer to Django, I am still learning the ropes. One challenge I am facing involves a multiselect list on my webpage. I need to send the selected items to my views for processing, and I attempted to accomplish this using Ajax. However, it seems that ...

Guide on implementing PHP.serialize for verifying a webhook in Ruby on Rails 5

Currently working with Ruby on Rails 5 and ruby -v 2.5.3, I'm in the process of verifying a webhook, following the instructions provided: require 'base64' require 'php_serialize' require 'openssl' public_key = '- ...

Upon making an Ajax call, the response returned as [object Object]

After making an Ajax call to an API, I received the following response: {"prova.com":{"status":0}} I attempted to retrieve the status value of 0 using this script: $.ajax({ type: 'GET', url: 'https://api.cloudns.net/domains/check- ...

Is there a way to create a JavaScript function that relies on a successful form submission?

After attempting to modify a post on Stack Overflow, I am facing issues with my JavaScript code. As a beginner, it's possible that I overlooked something in the code causing it not to work as expected. The project I'm working on involves creatin ...

Ajax is able to fetch the URL without any issues, but the page is not being updated as expected. Instead,

I am currently working on implementing next/prev buttons using JavaScript, and have utilized a DynamicPage script for testing purposes. The only modification I made was to the beginning of the URL variable in pagenumber.js, although it essentially delivers ...

Interpreting an undefined HTTP GET request within a Node.js server

I am encountering an issue within my Node.js application. When I send an http get request through an ajax call on the client-side, the server-side does not recognize the request data and returns an "undefined" error message. This problem is puzzling to me ...

Transferring data within a MongoDB collection using PHP without requiring admin privileges

I'm working on allowing a MongoDB user without admin privileges to execute collection copy in a PHP application. I know that db.source.copyTo(target) requires admin privilege, so instead I used db.getCollection(coll).find().forEach( function(d) {db.ge ...

Automatically redirecting to the designated page once the radio button option is submitted

I am encountering an issue with my form. I require it to redirect the user to different pages based on the selection of a radio button. The user must choose one of two options and click "next," and depending on their choice, they should be redirected to an ...

How to Use ng-controller in AngularJS to Submit a Form with PHP

Hey there! I'm diving into AngularJS and PHP for the first time. My current project involves creating a Contact Form using AngularJs and PHP. The form is pretty straightforward, requiring users to input their First Name, Last Name, Email, and Message. ...

Obtaining unique attributes from Laravel's many-to-many relationships

Currently, I am in the process of developing a module for my laravel web application that allows users to create multiple projects. These projects can have multiple contributors who need to be approved by the owners. The functionality for owner users has b ...

Asynchronous AJAX processing in Ruby helper functions

I need assistance with running an asynchronous ajax request in a Ruby on Rails helper method. Specifically, I am trying to use remote_function: remote_function :url => { :action => "draw_graph" }, :after => "do_something_after_ajax_is_done()" } ...

Show the most recent image from a folder in real-time

Looking for a solution to automatically display the latest image from a folder without refreshing the page? Check out this code snippet that achieves just that: <?php foreach (glob('/home/pi/camera/*.jpg') as $f) { $list[] = $f; } sort( ...

Determine the year of the end date with PostgreSQL

Context Users have the ability to select specific date ranges, which can be seen in the screenshot below: All combinations of starting month/day and ending month/day are valid, such as: Mar 22 to Jun 22 Dec 1 to Feb 28 The second combination presents ...

I need to know how to send a "put" request using JavaScript and Ajax

My task involves programmatically updating a spreadsheet using my code. I am able to write to a specific cell within the spreadsheet with the following function: function update(){ jQuery.ajax({ type: 'PUT', ...

Struggling to incorporate white spaces into my SQL query

Having trouble separating two variables in my WHERE statement, resulting in a syntax error. Any assistance is appreciated. (Using codeigniter) By the way, I've attempted using a $space variable and adding spaces before and after the 'AND' ...

What is the best way to directly access a child node for updating or removing?

This is an example of an xml file structure: <participants> <participant> <number>1</number> <name>toto</name> </participant> <participant> <number>2</number> <name>ti ...

What is the best way to replace the `eregi()` function with the `preg_match()` function in the given code snippet?

What method could be used to replace the eregi() function with the <code>preg_match() function in the following code snippet? function getOS($userAgent) { // Create list of operating systems with operating system name as array key $oses = a ...

What is the best method for converting a string picture URL into an image and showcasing it in an image tag?

I have a URL for an image that looks like this: C:\inetpub\MaujApp\OMSAPI\ManualReceivingImages\WhatsApp Image 2021-03-24 at 12.07.41 PM.jpeg My goal is to convert the original image and display it to the user using jQuery. I woul ...