How to send a get request to my other site?

People come to the site №1 in the link: site1.com/?g=X where X is any number.
I need to give this get-parameter (g=X) site No. 2 ( site2.com ) at the time of loading the first website.
I have access to both sites, and can implement any scripts.

As I understand it, to solve this problem, the # 1 site should do one of these things:
a) send a request to site # 2.
b) hidden go to site # 2.
C) to open/close the site in a new small window (not comme Il faut, and I understand that pop-UPS the browser can block.)
d) use some kind of API.

Which option is most appropriate and how to implement it? I'm a noob, so I would love any code :)
June 10th 19 at 14:43
4 answers
June 10th 19 at 14:45
Solution
AJAX:
var xhr = new XMLHttpRequest();
xhr.open("get", "https://site2.example.com/?g=x", true);
xhr.send();

More new stuff: fetch
fetch('https://site2.example.com/?g=x')(in older browsers is not a ride)

A completely bulletproof method:
<img src="https://site2.example.com/?g=x">this is also a GET request!

You can think of more ways, but it is not clear why.
I complete zero, so I have none of the options work :))

Here's what I added to site # 1 ( https://site1.com/ ):
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
 url: 'https://site2.com/',
 type: "GET",
 headers: {"Access-Control-Allow-Origin":"https://site2.com", 'Access-Control-Allow-Credentials': true},
 data: "g="+"1"
});

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://site2.com/?g=1', true);
xhr.send();

fetch('https://site2.com/?g=1')
</script>


Error in console when loading the site No. 1:
In the first case (via ajax): Failed to load https://site2.com/?g=1: Response to a preflight request doesn't pass the access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://site1.com' is therefore not allowed access.
In the second case (via xhr): Failed to load https://site2.com/?g=1: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://site1.com' is therefore not allowed access.
In the third case (using fetch): Uncaught (in promise) TypeError: Failed to fetch

What am I doing wrong? :)) - holly99 commented on June 10th 19 at 14:48
After implementing this code https://site2.com:
<?php header('Access-Control-Allow-Origin: *'); ?>
Method # 2 stopped giving the error in the console.
Checked through the code:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://site2.com/?ref=1', true);
xhr.onload = function() {
alert(xhr.responseText);
};
xhr.send();


In the alert issued message from extensions like AdBlock, so I disabled it. But, unfortunately, the desired result for which all this was started, and has not received.

The fact is that when the user logs in at: https://site2.com?g=1 website records certain information, including cookies to the user. But after the injection at site 1, the user accesses a website 1, and even sign them on site 2 (without get parameters), site 2 still does not contain the cookies of the user and information about his visit in the statistics. How to achieve this if the regular transition at https://site2.com?g=1 all records? - holly99 commented on June 10th 19 at 14:51
something you don't.
I just tried to send a request
https://jsfiddle.net/bdzck5z9/
getting No 'Access-Control-Allow-Origin' header is present - Austen.Thompson53 commented on June 10th 19 at 14:54
it I just removed from site No. 2 this code (just in case for security reasons):
<?php header('Access-Control-Allow-Origin: *'); ?>
Introduced back. Try again, please. - holly99 commented on June 10th 19 at 14:57
https://oiz.by/wp-test/?ref=1 and https://oiz.by/wp-test/ put two cookies:
eviU8E0.png

https://toptrening.org/?ref=1 it gives an error 500 - Austen.Thompson53 commented on June 10th 19 at 15:00
,
Introduced back. Try again, please.

Cookies are set: i80A5Vu.png - Austen.Thompson53 commented on June 10th 19 at 15:03
open in browser:
https://oiz.by/wp-test/ - 2 cookies
https://oiz.by/wp-test/?ref=1 - 2 cookies
check on jsfiddle ( https://jsfiddle.net/bdzck5z9/ ) - also 2

Conclusion: js is not to blame, and functioning properly. - Austen.Thompson53 commented on June 10th 19 at 15:06
maybe You made a mistake?
The second site with get parameter yields 4 cookies. I have deleted and 6 browsers I tried.
The same can not be such that I-4 e, and You have 2 :)
5a36f27550fba243260075.png - holly99 commented on June 10th 19 at 15:09
most often two, occasionally there is a third
grrDkTV.png - Austen.Thompson53 commented on June 10th 19 at 15:12
may be You have something blocking?! Type the extension that anonymizes the client or truncates a referral link (ref).
I'm with 3 devices tested, with 6 browsers, and even through proxies. Everywhere assigned to the desired cookies.
You do the same, as far as I can see - only session cookies are displayed.

If you check in Network tab, then going to site # 2 ?ref=1, selecting the tab ?ref=1, I get track. headers: Cookie:PHPSESSID=uogvpn3re5i8ivkoddpnaninb2; affwp_ref=1; affwp_ref_visit_id=-2; woocommerce_cart_hash=null

And in JSFiddle and the # 1 site only:
Set-Cookie:PHPSESSID=hho2504it6v2nfsacf9g771nl4; path=/

In the script settings I chose another method of tracking. And transfer the cookies will work!
Thank you so much for the help! I would be grateful if you try to visit the site and say, do You have cookies. Maybe this problem was solved by changing the method of tracking :) - holly99 commented on June 10th 19 at 15:15
now put.
And in jsfiddle:
9KHk24W.png
In the script settings I chose another method of tracking. And transfer the cookies will work!
What is "in the script settings"? - Austen.Thompson53 commented on June 10th 19 at 15:18
third-party script that set these cookies, appear in the statistics of the transition and everything else. - holly99 commented on June 10th 19 at 15:21
June 10th 19 at 14:47
Solution
There is another working method (in php):
file_get_contents('https://site2.com/?g='.$_GET['g']);

But unlike the xhr request, it refers to "direct traffic" that gives incorrect stats in the scripts.
XHR requests, on the contrary, the statistics will reflect a link with which was a request (which is much easier). Although, maybe it's a problem of the scripts.
You can configure the context and pass the referer header specifying therein any link. She will be displayed instead of "reference, which was a request" - holly99 commented on June 10th 19 at 14:50
June 10th 19 at 14:49
June 10th 19 at 14:51
take a look, maybe there you will find the solution
https://habrahabr.ru/post/120336/

Find more questions by tags JavaScriptPHPjQueryWordPress