Search code examples
javascriptjqueryajaxrequestclient-side-scripting

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?


Solution

  • 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.