按钮生成一个链接,然后使用ajax,php,javascript去那里


Button to generate a link, then to go there with ajax, php, javascript

我需要一个按钮来通过ajax调用php页面。我需要按钮来打开带有mailto链接的客户电子邮件。php 页面生成电子邮件,其中包含将凭据传递到安全站点的加密字符串。

基本上,我需要按以下顺序发生这些事情:

单击按钮。进行 ajax 调用。用"mailto:subject=your_secret_link&body=http://securesite.com?authcode=encrytpedstuff"填充href打开客户的电子邮件。

我试过这个:网页部分:

<span id="mailframe"><a href=# id="myemail"><input name="Request Signing via Email"  value="Request Signing via Email" type="button" class="redButton" onclick="sendEmail();"/></a></span>

JavaScript 部分

function sendEmail() {
    var hash=document.getElementById('hash').value;
    var obj=document.getElementById('mailframe');
    var email=document.getElementById('myemail');
    var mailxml = new XMLHttpRequest;
    mailxml.onreadystatechange = function() {
        if ((mailxml.readyState == 4) && ((mailxml.status == 302)|| (mailxml.status == 200))) {
            email.href=mailxml.responseText;
        }
    }
    mailxml.open("GET",'/secure/literature/generateid.php?hash='+hash+'&docid=<?=$docid?>' );
    mailxml.send();
}

它适用于IE,但不适用于Firefox。有什么想法吗?

我认为你需要这样的东西(适用于FireFox 24):

    <script type="text/javascript">
        function sendEmail() {
            var hash=document.getElementById('hash').value;
            var email=document.getElementById('myemail');
            var mailxml = new XMLHttpRequest;
            mailxml.onreadystatechange = function() {
                if (mailxml.readyState == 4 && ((mailxml.status == 302) || (mailxml.status == 200))) {
                    window.location.href = mailxml.responseText;
                }
            };
            mailxml.open("GET", "/your/long/url?with=parameters", true);
            mailxml.send();}
    </script>
    <div>
        <input type="button" onclick="sendEmail()" value="Send E-mail"/>
        <input type="text" name="hash" id="hash" value="hashValue" />
        <a href="#" id="myemail">E-mail link</a>
    </div>

另请查看检索跨浏览器 XmlHttpRequest 和代码中的大括号的最简单方法。