How to read the post request parameters using JavaScript

后端 未结 18 1156
自闭症患者
自闭症患者 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:02

    If you're working with a Java / REST API, a workaround is easy. In the JSP page you can do the following:

        <%
        String action = request.getParameter("action");
        String postData = request.getParameter("dataInput");
        %>
        <script>
            var doAction = "<% out.print(action); %>";
            var postData = "<% out.print(postData); %>";
            window.alert(doAction + " " + postData);
        </script>
    
    0 讨论(0)
  • 2020-11-22 17:03

    POST is what browser sends from client(your broswer) to the web server. Post data is send to server via http headers, and it is available only at the server end or in between the path (example: a proxy server) from client (your browser) to web-server. So it cannot be handled from client side scripts like JavaScript. You need to handle it via server side scripts like CGI, PHP, Java etc. If you still need to write in JavaScript you need to have a web-server which understands and executes JavaScript in your server like Node.js

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

    We can collect the form params submitted using POST with using serialize concept.

    Try this:

    $('form').serialize(); 
    

    Just enclose it alert, it displays all the parameters including hidden.

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

    JavaScript is a client-side scripting language, which means all of the code is executed on the web user's machine. The POST variables, on the other hand, go to the server and reside there. Browsers do not provide those variables to the JavaScript environment, nor should any developer expect them to magically be there.

    Since the browser disallows JavaScript from accessing POST data, it's pretty much impossible to read the POST variables without an outside actor like PHP echoing the POST values into a script variable or an extension/addon that captures the POST values in transit. The GET variables are available via a workaround because they're in the URL which can be parsed by the client machine.

    0 讨论(0)
  • 2020-11-22 17:06
    <head><script>var xxx = ${params.xxx}</script></head>
    

    Using EL expression ${param.xxx} in <head> to get params from a post method, and make sure the js file is included after <head> so that you can handle a param like 'xxx' directly in your js file.

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

    One option is to set a cookie in PHP.

    For example: a cookie named invalid with the value of $invalid expiring in 1 day:

    setcookie('invalid', $invalid, time() + 60 * 60 * 24);
    

    Then read it back out in JS (using the JS Cookie plugin):

    var invalid = Cookies.get('invalid');
    
    if(invalid !== undefined) {
        Cookies.remove('invalid');
    }
    

    You can now access the value from the invalid variable in JavaScript.

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