jQuery - No Access Control Allow Origin - php

I'd like to load a site from a different domain. I've already set headers through php in my header.php file:
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: *");
I've searched around for the correct way to do the ajax request with cross domain enabled and ended up with this:
$.ajax(
{
type: 'GET',
url: target,
processData: true,
data: {},
dataType: "json",
success: function (data)
{
$("#toolsarea").attr('src', target);
}
});
but I still get the error "No 'Access-Control-Allow-Origin". Is there still something I'm missing?

Your issue is related to Same origin policy which prevent JavaScript to make an AJAX request for security reasons.
You need to make sure CORS is enabled on your PHP server.
You can do it using:
<?php
header("Access-Control-Allow-Origin: *")
More information on how to enable CORS on your server can be found here:
http://enable-cors.org/server_php.html
You can read more about Same-origin policy on the client here:
https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

Related

jQuery AJAX http GET cross domain - required headers in response

I've made a web application running on domain http://app.mydomain.tld
and I've APIs on https://api.mydomain.tld (APIs are not developed by me).
I'm doing an AJAX HTTPs GET call with jQuery using this method:
$.ajax({
url: "https://api.mydomain.tld/GetSomething/read.php",
method: "GET",
contentType:"application/json; charset=utf-8",
dataType:"json",
async:false,
headers: {"Accept": "application/json; odata=verbose" },
success: function (data) { doSomething(); },
error: function (data) { showError(); }
});
This call is returning an error like NetworkError: failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://api.mydomain.tld/GetSomething/read.php'.
I've added the Access-Control-Allow-Origin extension for Chrome and the call is working fine.
So I've checked response headers. When Chrome extension is NOT enabled I've this response headers:
Access-Control-Allow-Origin: *
When the extension is enabled I've these headers:
Access-Control-Allow-Headers: access-control-allow-methods,access-control-allow-origin
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, HEAD, OPTIONS
Access-Control-Allow-Origin: *
So my question, that could have an obvious answer, is: are these headers required to make correct cross domain calls?
If yes, do my APIs need the below additional code?
<?php
header("Access-Control-Allow-Origin","*");
header("Access-Control-Allow-Methods","GET, PUT, POST, DELETE, HEAD, OPTIONS");
header("Access-Control-Allow-Headers","access-control-allow-methods,access-control-allow-origin");
?>
Is correct what I said or am I missing something to make my call working without changing APIs code?
PS: if possibile I would not use JSONP.

Custom headers for ajax and php

So, I'm having a problem retrieving custom headers from ajax call.
This is my ajax call:
$.ajax({
url: 'api.php',
type: 'get',
processData: false,
beforeSend: function(xhr) {
xhr.setRequestHeader('HASH', '5c268592cd4db9c7f6b813bb689005c6');
},
success: function(data) {
console.log(data);
},
error: function(xhr, status, error) {
console.log(xhr);
}
});
And in my api.php, I have this:
<?php
$headers = getallheaders();
print_r(json_encode($headers));
the output:
......
"Access-Control-Request-Headers":"content-type,hash",
....
This returns null:
echo $headers['hash'];
// or this echo $headers['HASH'];
You are making a cross-origin request (you shouldn't be, since your code shows a relative URL, but perhaps there is an HTTP redirect in play somewhere) and adding custom headers.
This means that it is not a simple request, but requires a preflight OPTIONS request.
The output you see is the headers of the OPTIONS request asking permission from the server to send the request with the custom headers.
You need to grant that permission, then the browser will make a second request (which is the one you are expecting).
header("Access-Control-Allow-Origin: http://example.com/");
header("Access-Control-Allow-Headers: HASH, Content-Type");
header("Access-Control-Allow-Methods: GET");
Try this $_SERVER["HTTP_HASH"];

Classic cross domain ajax with jquery client side PHP server side

It is about a javascript pixel to follow up who does what in a sales funnel on the web.
I have a javascript script on my customer thrivecart domain (e.g. https://ownspace.thrivecart.com)
I want to make a request to another domain (e.g. https://emails.mycustomer.com) from which the javascript script is from.
Here is the main part of the javacsript code on https://ownspace.thrivecart.com :
$(document).ready(function () {
console.log("loading pixel");
$.ajax({
url: 'https://emails.mycustomer.com/server_side_script.php',
type: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest'},
data: {my:ciphered_get_parameters},
success: function (result) {
console.log(result);
}
});
});
Here is what I have server side for the moment : (server_side_script.php)
<?php
header('Content-Type:application/json');
header("Access-Control-Allow-Origin:https://ownspace.thrivecart.com");
header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With, Accept");
// Special data treatment
I get this error on the thrivecart page :
Failed to load https://emails.mycustomer.com/server_side_script.php: Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers in preflight response.
However, I read that "
Access-Control-Allow-Headers :
Indicates which headers are supported by the response’s url for the purposes of the CORS protocol."
As the header is on in the PHP code, I don't understand why it does not work.
I even tried to set the X-Requested-With header in the response with NGINX conf file, restarting the server.
But, I think I miss a point.
Appears to be a typo in
header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With, Accept");
Try
header("Access-Control-Allow-Headers:Content-Type, Authorization, X-Requested-With, Accept");

Header information's not passing for cross domain request

I have made one API application, and using jQuery ajax I am trying to get data from API. But I am getting error 401: Authorization Error. Later I found the header Authorization is not passing with request, so API cannot able to track it. Below is the code which I used for my application. Please help me...
PHP Code:
header("Access-Control-Allow-Orgin: *");
header("Access-Control-Allow-Methods: *");
header("Access-Control-Allow-Headers: Authorization, X-Requested-With, Content-Type");
header("Access-Control-Allow-Credentials: true");
jQuery Code:
$.ajax({
type: method,
url: url,
dataType: 'JSONP',
crossDomain: true,
headers: {'X-Requested-With': 'XMLHttpRequest'},
beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', "Basic xxxx");
},
success: function (){
alert('Thanks for your comment!');
}
});

CORS and Cookies working together with PHP headers and jQuery

I'm trying to use php headers to accept cookies and various types of ajax requests to my api. Also the request can come from any server, so I can't whitelist them. I'd like to use
header("Access-Control-Allow-Origin: *");
but this blocks cookies. Is there a way around this?
Here's the setup for the ajax calls with jquery:
$.ajax({
type: 'POST',
data: $('#loginForm').serialize(),
crossDomain:true,
dataType: 'json',
beforeSend: function(xhr){
xhr.withCredentials = true;
},
url: "https://myothersite.com/login_submit.php"...
You can't set or read cookies on cross origin resource sharing requests through JavaScript. Cookies respect the browser's same origin policy.

Categories