How to read the post request parameters using JavaScript

后端 未结 18 1158
自闭症患者
自闭症患者 2020-11-22 16:41

I am trying to read the post request parameters from my HTML. I can read the get request parameters using the following code in JavaScript.

$wnd.location.sea         


        
相关标签:
18条回答
  • 2020-11-22 17:12

    A little piece of PHP to get the server to populate a JavaScript variable is quick and easy:

    var my_javascript_variable = <?php echo json_encode($_POST['my_post'] ?? null) ?>;
    

    Then just access the JavaScript variable in the normal way.

    Note there is no guarantee any given data or kind of data will be posted unless you check - all input fields are suggestions, not guarantees.

    0 讨论(0)
  • 2020-11-22 17:14

    POST data is data that is handled server side. And Javascript is on client side. So there is no way you can read a post data using JavaScript.

    0 讨论(0)
  • 2020-11-22 17:14

    Why not use localStorage or any other way to set the value that you would like to pass?

    That way you have access to it from anywhere!

    By anywhere I mean within the given domain/context

    0 讨论(0)
  • 2020-11-22 17:14
    function getParameterByName(name, url) {
                if (!url) url = window.location.href;
                name = name.replace(/[\[\]]/g, "\\$&");
                var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                    results = regex.exec(url);
                if (!results) return null;
                if (!results[2]) return '';
                return decodeURIComponent(results[2].replace(/\+/g, " "));
            }
    var formObj = document.getElementById("pageID");
    formObj.response_order_id.value = getParameterByName("name");
    
    0 讨论(0)
  • 2020-11-22 17:15

    You can read the post request parameter with jQuery-PostCapture(@ssut/jQuery-PostCapture).

    PostCapture plugin is consisted of some tricks.

    When you are click the submit button, the onsubmit event will be dispatched.

    At the time, PostCapture will be serialize form data and save to html5 localStorage(if available) or cookie storage.

    0 讨论(0)
  • 2020-11-22 17:16

    Use sessionStorage!

    $(function(){
        $('form').submit{
           document.sessionStorage["form-data"] =  $('this').serialize();
           document.location.href = 'another-page.html';
        }
    });

    At another-page.html:

    var formData = document.sessionStorage["form-data"];

    Reference link - https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

    0 讨论(0)
提交回复
热议问题