I have tried this code for sending some information to a specific IP. That IP is a microcntroller that acts as a server.
However it sends the information to a page named with that IP not to that IP.
The code is written in JavaScript. What I should to do? Use post method or Xmlhttprequest and how to do that. 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. If actually you want to remain on the same page, send information - visa versa then indeed an AJAX call would be most sufficient. 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.
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 :
/**
* 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.