简体   繁体   中英

How to get a url from a php hyperlinlk with post data

I'm working on a downloads page which is using DLGuard to create custom file download URLs after a shop purchase.

For example:

<a href="downloadfile.php?r=11034705&p=42">Download</a>

would be the hyperlink on the page but clicking this would direct you to the url:

foo.s3.amazonaws.com/example.mp3?AWSAccessKeyId=...

My question is it possible to find out the url of the .mp3 without having to navigate off the page using an ajax call? So far I have tried the following code:

<script>
    var downloadURL = $("a.button").attr('href');
    console.log(downloadURL);

$.ajax({
    type: 'GET',
    url: downloadURL,
    success: function () {
        alert("done!");
    }
});

</script>

But only get the following error message:

XMLHttpRequest cannot load http://foo.s3.amazonaws.com/example.mp3 ... Origin http://www.example.co.uk is not allowed by Access-Control-Allow-Origin.

All I want is the url of the .mp3 file stored as a string. Can anyone help?

You would need a backend to do a webservice call due to Cross-Domain request .

Browsers do not let you hit another domain using JavaScript.

But, you can create a basic web service using PHP, Rails, Node.js, etc. that will make the call for you. So you call your own web service using AJAX, which calls the URL you want (there are many libraries that make this easier). Then have your web service pass the results back to you, which you will retrieve the data in the AJAX success callback.

This is fairly trivial using PHP, Rails or Node.js and you should have no problem finding examples online.

Make your ajax call to your server on same domain. Then in the php do this:

$page_data = file_get_contents('foo.s3.amazonaws.com/example.mp3?AWSAccessKeyId=...');
echo $page_data;

or any other kind of php get request using curl, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM