Tunjay
Tunjay

Reputation: 110

How to send data with GET method to another domain?

My Javascript file generates a JSON and there are some necessary values which I want to send it to the link www.leaderpush.com/Send/Getjson?Endpoint=endpoint&P256dh=p256dh&Auth=auth

var obj = JSON.parse(t);
var endpoint = obj.endpoint;
var p256dh = obj.keys.p256dh;
var auth = obj.keys.auth;

But I cannot send it to another domain. When I try, the URL that is sent becomes www.AnyDomain.com/www.leaderpush.com/Send...

What is your suggestion?

Upvotes: 1

Views: 489

Answers (2)

Balaji
Balaji

Reputation: 19

Since you have not mentioned the protocol absolutely(HTTP / HTTPS) or relatively(//) in the request URL, the browser treats it as a path and appends the request URL after the origin domain. And the request domain does not seem to support and have valid HTTPS certificate and hence make sure your origin domain is HTTP and the request domain (//www.leaderpush.com) is CORS supported.

Note: Try including crossDomain: true in $.ajax() request header, in case the target server might serve on request to enable cross domain to the client.

Upvotes: 1

Barmar
Barmar

Reputation: 780843

You need to format the URL correctly.

url = "//www.leaderpush.com/Send/Getjson?Endpoint=endpoint&P256dh=p256dh&Auth=auth";

You need // at the beginning to indicate that the domain name of the URL follows, otherwise it's treated as a filename relative to the current URL.

BTW, this still might not work if the other domain prohibits CORS. You may need to make the request from your server rather than from the client.

Upvotes: 3

Related Questions