javascript - POST to web service on page load - Stack Overflow

I'm trying to send some data to a web service on page load and then allow the page to continue to

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
          }
        });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745280684a4620262.html

相关推荐

  • javascript - POST to web service on page load - Stack Overflow

    I'm trying to send some data to a web service on page load and then allow the page to continue to

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信