0

while i was working in localhost my web share code is working perfectly but when i hosted my website same code stopped working. my code is still working in localhost server but not in hosted server.

this is my code:

<li class="post-menu-item share_btn" id="'.$post_id.'">Share</li>

actually it is written under php so it has id="'.$post_id.'" in it.

this is my web share API code:

$(".share_btn").click(function(e) {
const post_id = $(this).attr("id");
const link = 'http://localhost/hypeup/post/' + post_id + '/';
if (navigator.share) {
    navigator.share({
            title: 'My awesome post!',
            text: 'This post may or may not contain the answer to the universe',
            url: link,
        }).then(() => {
            console.log('Thanks for sharing!');
        })
        .catch(err => {
            console.log(`Couldn't share because of`, err.message);
        });
} else {
    console.log('web share not supported');
}});
ritik soni
  • 11
  • 2
  • Your link ````http://localhost/hypeup/post```` has a hard coded "localhost", did you also update that to your new domain name? –  Oct 31 '21 at 17:01
  • Change localhost/hypeup with your domain address – keyhan Oct 31 '21 at 17:02
  • I have already done that but it's still not working. – ritik soni Oct 31 '21 at 18:47
  • (It would be semantically nicer to use a ` – DenverCoder9 Nov 09 '21 at 11:47
  • Actually it started working my api is not working because at that point I didn't have HTTPS I was using HTTP for my website. Share API only works in HTTPS website......... www.postvely.com this is my website you can check it out – ritik soni Nov 10 '21 at 12:13

1 Answers1

2

The Web Share API is only available in secure contexts, that is, when your page is loaded over the https: protocol. When you develop locally, the browser considers localhost and 127.0.0.1 etc. to be secure contexts, so for development purposes you don't need to set up https:, only for the production site. You should configure your web server so it redirects people from http: to https:, you can find many ways of doing this summarized in this article.

DenverCoder9
  • 2,024
  • 11
  • 32