如何在JavaScript中编码拆分的URL?

时间:2019-04-25 17:50:40

标签: javascript

您好,我正在尝试用javascript编码拆分的url。我要编码的实际部分是explode [1]。 我举一个例子,可以说fullURL是

https://hey.com/xyz.php?https://mega.nz/#!link!pass

爆炸[1]将是

https://mega.nz/#!link!pass

并且api_url var将完全是

https://alo.pm/api?api=myapitoken&url=https://mega.nz/#!link!pass&format=text

但是我希望api_url是

https://alo.pm/api?api=myapitoken&url=https%3A%2F%2Fmega.nz%2F%23%21link%21pass%26format%3Dtext

我希望我能解释我的问题。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
var fullURL = window.location.href;
var explode = fullURL.split('?');
document.write(explode[1]); // mega url
var api_token = 'myapitoken';
var api_url = "https://alo.pm/api?api="+ api_token +"&url="+ explode[1] +"&format=text"; $.ajax ({ type : "GET", url:api_url, data:{}, dataType: "json", async: false, // Cevap alındıktan sonra kodlar çalışmaya devam etsin. Cevap gelene kadar beklesin
timeout: 3000, //zaman aşımı süresi 3 sn
success :function(donen_veri) { 
window.location.href = donen_veri.shortenedUrl; document.write(donen_veri.shortenedUrl); 
},
error:function(XMLHttpRequest, textStatus, errorThrown) { document.write('Hata = ' + textStatus); }, } )
</script>

0 个答案:

没有答案