[英]javascript sending data into a url
I have tried this code for sending some information to a specific IP. 我已尝试使用此代码将某些信息发送到特定IP。 That IP is a microcntroller that acts as a server.
该IP是充当服务器的微控制器。
However it sends the information to a page named with that IP not to that IP. 但是,它将信息发送到以该IP命名的页面,而不是该IP。
The code is written in JavaScript. 该代码是用JavaScript编写的。 What I should to do?
我该怎么办? Use post method or Xmlhttprequest and how to do that.
使用post方法或Xmlhttprequest及其操作方法。 I think my code is very simple:
我认为我的代码非常简单:
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="192.168.1.250" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
</html>
您需要包括协议
action="http://192.168.1.250"
If you were wanting to send the User to that IP as well, then you would use POST. 如果您也想将用户发送到该IP,则可以使用POST。 If actually you want to remain on the same page, send information - visa versa then indeed an AJAX call would be most sufficient.
如果实际上您希望保留在同一页面上,请发送信息(反之亦然),那么实际上AJAX呼叫就足够了。 Below I use vanilla JavaScript instead of any JavaScript libraries, although using jQuery would provide you with some callbacks/helpers to make your code more stable.
下面,我使用普通JavaScript而不是任何JavaScript库,尽管使用jQuery会为您提供一些回调/帮助程序,以使您的代码更稳定。
jsFiddle: http://jsfiddle.net/atjBQ/3/ jsFiddle: http : //jsfiddle.net/atjBQ/3/
<script>
/**
* Validate Form, else, Send Ajax
**/
function validateform() {
var x = document.forms["myForm"]["fname"].value;
if ( x == null || x == "" ) {
alert( "First Name must be filled out" );
return false;
}
/**
* If POST
* use: xmlhttp.setRequestHeader(
* "Content-type",
* "application/x-www-form-urlencoded"
* );
**/
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://192.168.1.250?q=" + x, true);
xmlhttp.send();
return false;
}
</script>
<form name="myForm" id="myformtosend">
<label for="fname">First name:</label><input type="text" name="fname" />
<input type="submit" value="Submit" />
</form>
With jQuery : 使用jQuery :
/**
* Snippet Reference to:
* http://api.jquery.com/jQuery.post/
**/
<script>
$.ajax({
type: "POST",
url: "http://192.168.1.250",
data: data,
success: function() {
/** Some Code **/
}
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.