通過JS組裝表單

來源:互聯網
上載者:User

標籤:js 組裝表單

情境:例如我們做一個支付功能,肯定需要與第三方支付公司進行對接,比如【網關支付】是需要跳轉到第三方的儲值頁面的,但是第三方只提供了後台介面,我們可以通過調用第三方的介面擷取到我們需要的資料,然後通過在前端通過JS組裝一個Form表單跳轉到第三方的儲值頁面去。


//業務處理跳轉網關支付頁面

function businessprocessToPay(data) {

   //這裡的Data是後台介面返回的資料

    if (data.status == 200) {

        var form = $(‘<form></form>‘);

        var messageHTML = $(‘<input type="hidden" name="message" value="" />‘);

        messageHTML.attr(‘value‘, data.data.PostMessage);

        var signatureHTML = $(‘<input type="hidden" name="signature" value="" />‘);

        signatureHTML.attr(‘value‘, data.data.PostSiginature);


        form.append(messageHTML);

        form.append(signatureHTML);

        form.attr(‘action‘, data.data.PayUrl);

        form.attr(‘method‘, ‘post‘);

        form.attr(‘target‘, ‘_blank‘);

        form.attr(‘style‘, ‘display:none‘);

        //HTML標準規定如果form表單沒有被添加到document裡,那麼form表單提交將會被終止

        $(document.body).append(form);

        form.submit();

        //刪除關聯

        $(document.body).remove(form);

    }else {

        alertmsg(data.msg, ‘error‘);

    }

}



本文出自 “Flyfish” 部落格,請務必保留此出處http://9381188.blog.51cto.com/9371188/1923753

通過JS組裝表單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.