admin管理员组

文章数量:1418136

I'm trying to send some data to a web service on page load and then allow the page to continue to work as usual. The idea is to grab some data from the client just when the page loads without user interaction and then let the user continue with the page normally.

I'm using XMLHttpRequest, when I wanted to do the POST to the web service without refreshing the page, I don't know if that's the best method. Here is my current test html:

<html>
<body>
    <form name="myform2" method="post" action="./login.asp">
        First Name: <input type="text" name="fname" /><br><br>
        Last Name : <input type="text" name="lname" />
        <input type="submit" value="Submit" />
    </form>


<form action="" id="myform">
    <input type="submit" value="send-info"/>
</form>

<script src=".2.1.min.js"></script>
<script type="text/JavaScript">

$(document).ready(function() {
    $(document).on('submit', '#myform', function() {
        var http = new XMLHttpRequest();

        var params = ("myvar=somedata");
        http.open("POST", "http://127.0.0.1:3000", true);

        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        http.send(params);
        return false;
     });
});
</script>

</body>
</html>

Right now the data is sent when I click the send-info button but I need to send it without user interaction, without refreshing the page and allowing whatever additional logic is needed after that. I have tried with window.onload without success.

Please excuse my poor programming skills, I'm not that versed on Javascript.

I'm trying to send some data to a web service on page load and then allow the page to continue to work as usual. The idea is to grab some data from the client just when the page loads without user interaction and then let the user continue with the page normally.

I'm using XMLHttpRequest, when I wanted to do the POST to the web service without refreshing the page, I don't know if that's the best method. Here is my current test html:

<html>
<body>
    <form name="myform2" method="post" action="./login.asp">
        First Name: <input type="text" name="fname" /><br><br>
        Last Name : <input type="text" name="lname" />
        <input type="submit" value="Submit" />
    </form>


<form action="" id="myform">
    <input type="submit" value="send-info"/>
</form>

<script src="https://code.jquery./jquery-2.2.1.min.js"></script>
<script type="text/JavaScript">

$(document).ready(function() {
    $(document).on('submit', '#myform', function() {
        var http = new XMLHttpRequest();

        var params = ("myvar=somedata");
        http.open("POST", "http://127.0.0.1:3000", true);

        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        http.send(params);
        return false;
     });
});
</script>

</body>
</html>

Right now the data is sent when I click the send-info button but I need to send it without user interaction, without refreshing the page and allowing whatever additional logic is needed after that. I have tried with window.onload without success.

Please excuse my poor programming skills, I'm not that versed on Javascript.

Share Improve this question asked Dec 24, 2017 at 18:01 Alfred RapozoAlfred Rapozo 131 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You should just be able to remove the "on submit" so your code runs when the "document ready" event is called. Also if you are using jquery you can use its ajax function to simplify the http call:

$(document).ready(function() {
  $.ajax({
    type: "POST",
    url: "http://127.0.0.1:3000",
    data: { "myvar" : "somedata" }
   });
});

Right now you are sending data on submit form. So you need to use ajax call right after your $(document).ready(function() Here is a sample

     $(document).ready(function()({
myvar = somedata;
 $.ajax({
          url: "test.html",
          type: "Post",
          data: {myvar : myvar},
          success: function(response){
            //do anything here
          }
        });
});

本文标签: javascriptPOST to web service on page loadStack Overflow