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 badges2 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - POST to web service on page load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745280684a2651407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论