【问题标题】:Redirect form to another page instead of API将表单重定向到另一个页面而不是 API
【发布时间】:2012-12-10 11:00:00
【问题描述】:

好的,这是我的情况。

我有一个 php 文件,其中包含一个简单的表单,询问姓名、号码等。 现在,当我单击提交时,我将表单操作设置为处理这些变量的 API 的 URL。

问题是,当我提交表单时,它会将我带到 API 网站使用一些乱码 xml 文本确认提交的页面。

我想做的是能够让用户填写表单数据,秘密地将该数据提交到 API URL,并为用户显示一个感谢页面。我不希望用户知道 API 的确认页面,只是提交表单,直接将他带到感谢页面。

API接受以下形式的请求

"MY-API-URL.com/json?api_key=KEY&api_secret=SECRET&login=LOGIN&password=PASSWORD"

这是表格标题..

<form action="MY-API-URL.com" class="login">

感谢任何帮助!

【问题讨论】:

    标签: php html xml forms api


    【解决方案1】:

    进行 ajax 调用以提交您的表单。

    像这样制作一个自我提交的表单action=""

    <form id="login" name="login" action="" method="POST">
        <input type="text" name="login" value="">
        <input type="password" name="password" value="">
        <input type="submit" name="submit" value="Log in">
    </form>
    

    使用 jQuery 处理表单的提交事件:

    <script>
    $(function(){
        $("form#login").submit(function(){
            var login = $("input[name=login]").val();
            var password = $("input[name=password]").val();
    
            $.ajax({
                url: "MY-API-URL.com/json", 
                type: "POST",
                data: {"api_key":"KEY", "api_secret":"SECRET", "login":login, "password":password},
                dataType: "jsonp",
                cache: false,
                success: function (data) {              
                    //make your redirect here or just display a message on the same page
                    window.location = "congrats.html";
                },
                error: function(jqXHR, textStatus, errorThrown){
                    // handle your error here
                    alert("It's a failure!");
                }       
            });
            //cancel the submit default behavior
            return false;
        });
    });
    </script>
    

    更新:
    据我了解 nexmo 不支持 jsonp 并且您不能使用 json 因为您正在进行跨域调用。 这里有很多关于它的帖子。例如json Uncaught SyntaxError: Unexpected token :

    作为一种解决方法,您可以使用代理。你可以阅读它here 并下载简单的代理here

    如果您使用上面提到的代理,您的代码将如下所示:

    <script> 
    $(function(){ 
        $("form#sms").submit(function(){ 
            var from = $("input[name=from]").val(); 
            var to = $("input[name=to]").val(); 
            var text = $("input[name=text]").val(); 
    
            var url = "http://rest.nexmo.com/sms/json?api_key=key&api_secret=secret" + "&from=" + from + "&to=" + to + "&text=" + text; 
            $.ajax({ 
                url: "simple-proxy.php", 
                type: "GET", 
                data: {"url": url}, 
                dataType: "json", 
                cache: false, 
                success: function (data) {               
                    //make your redirect here or just display a message on the same page 
                    console.log(data); 
                    if (data && data.contents && data.contents.messages && data.contents.messages.length) {
                        alert("The status is: " + data.contents.messages[0].status);
                    }
                    alert("SMS sent!"); 
                }, 
                error: function(jqXHR, textStatus, errorThrown){ 
                    // handle your error here 
                    alert("textStatus: " + textStatus + "\n" + "errorThrown: " + errorThrown); 
                }       
            }); 
            //cancel the submit default behavior 
            return false; 
        }); 
    }); 
    </script> 
    

    我让它在我的机器上运行,它返回了正确的 json 响应。

    【讨论】:

    • 好吧,我的 API 使用 www.apiwebsite.com/json?variable1=something&variable2=something .. 我在使用 ajax 将该字符串提交到远程服务器时遇到了问题。. 有什么想法吗?
    • 如果它与您的域不同,您需要使用dataType: "jsonp"。欲了解更多信息,请参阅jQuery.ajax()
    • 好吧,我试过上面的,没有用..页面刚刚刷新!有任何想法吗?到目前为止,这是我的代码.. pastebin.com/ivL9XVNV
    • 那是因为您没有包含指向 jQuery 的链接。抱歉,我认为这很明显。为此,您需要在&lt;link rel="stylesheet" href="css/style.css"&gt; 之后添加以下行&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt;。我已将它添加到您的 patbin 中。
    • peterm,它可以工作,但我对成功回复有疑问,虽然发送了消息,但它总是失败。这是我从 API 服务器得到的回复: {"message-count":"1","messages":[{"to":"number","message-price":"price","status": "0","message-id":"msgid","re​​maining-balance":"balance","network":"network"}]} 状态部分是回复成功为0,失败为其他数字。有什么想法吗?
    猜你喜欢
    • 2014-05-17
    • 2014-08-10
    • 1970-01-01
    • 2016-02-28
    • 2017-06-05
    • 2017-08-10
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    相关资源
    最近更新 更多