POST PASSING PARAMETERS IN JAVASCRIPT

Hi!
I need todo a POST to the URL www.someserver.com/somesite/somepage.aspx passing the following parameters:  a=1, b=2 and c=3.
Can anybody helps me posting a JavaScript coded function in order to be called pressing a button?

Thanks in advance,
Leonel
leorochaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I tend to do this in jquery, if you need pure js, let me know.

Using ajax meaning you don't have to have a page refresh, you need to post to the same domain. http://api.jquery.com/jquery.ajax/

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script>
    $(function() {
    $("#clickme").on("click", function() {

        $.ajax({
                method: "POST",
                url: "www.someserver.com/somesite/somepage.aspx ",
                data: {
                    a: "1",
                    b: "2",
                    c: "3"
                }
            })
            .done(function(data) {
                //alert( "returned value from somepage.aspx: " + data );
            });
    });
});
  </script>
  <meta charset="utf-8">
  <title>experts-exchange.com/questions/28695168/</title>
</head>
<body>
  <button id="clickme">Submit</button>
  
  
</body>
</html>

Open in new window

Alternatively you can post a form like using .submit
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script>
    $(function() {
    $("#clickme").on("click", function() {
        $("#form1").submit();
      
    });
});
  </script>
  <meta charset="utf-8">
  <title>experts-exchange.com/questions/28695168/</title>
</head>
<body>
  <form id="form1">
    <input type="hidden" name="a" value="1">
    <input type="hidden" name="b" value="2">
    <input type="hidden" name="c" value="3">
  
  </form>
  <button id="clickme">Submit</button>
</body>
</html>

Open in new window

leorochaAuthor Commented:
Thanks Scott. I really need pure JS.

FYI, I'm trying to POST to that external page from my app aspx page.
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Give this one a try

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>experts-exchange.com/questions/28695168</title>
  <script>
//https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick
    function initElement()
 {
 var p = document.getElementById("clickme");

 p.onclick  = submitForm;
 }

    
    function submitForm() {
    document.getElementById("form1").submit();
}
  </script>
</head>
<body onload="initElement()">
 <form id="form1">
    <input type="hidden" name="a" value="1">
    <input type="hidden" name="b" value="2">
    <input type="hidden" name="c" value="3">
  
  </form>
  <button id="clickme">Submit</button>
</body>
</html>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Dave BaldwinFixer of ProblemsCommented:
This page http://www.javascriptkit.com/dhtmltutors/ajaxgetpost2.shtml shows how to do an AJAX POST in javascript.  You will have to adapt your variables where it shows 'parameters'.  If there is any chance that they will contain special characters, you need to use the 'encodeURIComponent' method.
Dave BaldwinFixer of ProblemsCommented:
leorochaAuthor Commented:
Sorry Scott for my ignorance, but where is the POST method, and the URL?.
hieloCommented:
>> Sorry Scott for my ignorance, but where is the POST method, and the URL?.
with Scott's suggestion, the method and url need to be coded in the form:
<form id="form1" method="post" action="url">
<input type="hidden" name="a" value="1">
    <input type="hidden" name="b" value="2">
    <input type="hidden" name="c" value="3"></form>

Open in new window

Since he is using hidden fields, these will not be displayed on the page.

Dave's suggestion above (post ID: 40869895) is probably what you are after.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.