简体   繁体   English

基于ajax调用的动态href网址

[英]dynamic href url based on ajax call

Really not sure how to even go about asking this question but it is something like this. 真的不知道该怎么问这个问题,但这是这样的。

How can I run some type of ajax(php script on server end) based on user's onclick action and dynamically launch url based on return from ajax and put it in href effectively launching that url? 如何基于用户的onclick动作运行某种类型的ajax(服务器端的php脚本),并根据ajax的返回动态启动url并将其置于href中,从而有效地启动该url?

so I want to say something like 所以我想说些类似的话

enter code here

<a href="urlYetTobeDeterminedAtTheTimeOfOnClick" onclick=sometfunctionWhichREturnsUrl>

And say I have a php function which returns site:a.com if it's before midnight to 8am, site:b.com if it's 8am to noon and site:c.com if it's noon to midnight. 并说我有一个php函数,如果它是在午夜之前至上午8点,则返回site:a.com,如果是中午8点则返回site:b.com,如果是中午至午夜则返回site:c.com。 I don't need a exact code but some sort of direction of how this can be arranged.. I have done lot of research on google but I do not see anything so far that I can use or point me to right direction. 我不需要确切的代码,但是需要某种方向的排列方式。.我在google上进行了很多研究,但到目前为止我还没有发现任何可以使用或指向正确方向的东西。 Can someone please advise? 有人可以请教吗?

thank you. 谢谢。

I'd just have the anchor point to the a url that determines the address to go to and then let it call a redirect to the url. 我只是将锚点指向一个url,该url确定要转到的地址,然后让它调用到该URL的重定向。

<a href="script.php">

script.php script.php

<?php
$url = get_the_url_function();
header('Location: '.$url);
exit;

You wouldn't put anything in the href. 您不会在href中放入任何内容。 You will do an ajax request to the endpoint responsible for the business logic behind deciding where to send the user. 您将在决定将用户发送到何处之后,向负责业务逻辑的端点发出ajax请求。

That endpoint will return a json object which includes a value that has the url to send the user to. 该端点将返回一个json对象,该对象包含一个具有将用户发送到的url的值。 You will check for a successful response, if success then window.location({insert value here}); 您将检查是否有成功的响应,如果成功,则检查window.location({insert value here});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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