javascript 使用AJAX直接发送html5 textarea不带html <form>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21539305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use AJAX to send html5 textarea directly without html <form>
提问by user3267738
Recently I am confused about whether it's possible to send input/textarea data directly without being included in html < form >. I thought in web page, if we want to get information from user then send the text to authentication server, we must use < form > irrespective of in which way it's submitted.
最近我对是否可以直接发送 input/textarea 数据而不包含在 html <form> 中感到困惑。我想在网页中,如果我们想从用户那里获取信息然后将文本发送到身份验证服务器,我们必须使用<form>,无论它以哪种方式提交。
But an anonymous reviewer of my paper claims that < html > can be bypassed by using an html5 tag "textarea" and JS AJAX post. While I did lots of experiments trying to implement his way but all failed.
但是我论文的匿名审稿人声称可以通过使用 html5 标签“textarea”和 JS AJAX 帖子绕过 < html >。虽然我做了很多实验试图实现他的方式,但都失败了。
I wonder if there is really some way to submit user info without using < form > tag?
我想知道是否真的有某种方法可以在不使用 <form> 标签的情况下提交用户信息?
Thank you
谢谢
----------------------------------------------------------------------------------------------------------
-------------------------------------------------- -------------------------------------------------- ------
Thanks for everyone's reply.
谢谢大家的回复。
Update:I followed "the_void"'s code and changed the url of AJAX to a ServerSocket (implemented by Java). The server was able to get the POST event, but it cannot read the "data" of AJAX. The following is the html code:
更新:我按照“the_void”的代码将 AJAX 的 url 更改为 ServerSocket(由 Java 实现)。服务器能够获取 POST 事件,但无法读取 AJAX 的“数据”。以下是html代码:
HTML
HTML
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function() {
//information to be sent to the server
info = $('#foo').val();
$.ajax({
type: "POST",
url: 'http://10.0.0.3:8888',
data: ({foo: info}),
//crossDomain: true,
//dataType: 'json'
});
return false;
});
});
</script>
</head>
<body>
<label>Text</label>
<textarea id="foo"></textarea>
<button id="submit">Submit via Ajax</button>
</body>
</html>
It seems that the socket server cannot read from AJAX (but it can read from < form > + < action >). Is there any way to fix the reading issue?
似乎套接字服务器无法从 AJAX 读取(但它可以从 <form> + <action> 读取)。有没有办法解决阅读问题?
Thank you
谢谢
回答by kabirbaidhya
Ajax (Asynchronous Javascript & XML) is a way to send data from client to the server asynchronously. For that you'd have to write code for sending the data in the client-side using Javascript/HTML and also to process the received data using server-side languages (eg PHP) on the server-side.
Ajax(Asynchronous Javascript & XML)是一种将数据从客户端异步发送到服务器的方法。为此,您必须编写代码以使用 Javascript/HTML 在客户端发送数据,并在服务器端使用服务器端语言(例如 PHP)处理接收到的数据。
And, yes you don't need a <form>
tag to do so.
而且,是的,您不需要<form>
标签来这样做。
Check out this example.
看看这个例子。
HTML:
HTML:
<label>Text</label>
<textarea id="foo"></textarea>
<button id="submit">Submit via Ajax</button>
Javascript:
Javascript:
$('#submit').click(function(e) {
e.preventDefault();
// information to be sent to the server
var info = $('#foo').val();
$.ajax({
type: "POST",
url: 'server.php',
data: {foo: info}
});
});
Server-side Handler (PHP):server.php
服务器端处理程序(PHP):server.php
<?php
// information received from the client
$recievedInfo = $_POST['foo'];
// do something with this information
See this for your reference http://api.jquery.com/jquery.ajax/
回答by darktrek
Perhaps your reviewer was referring to the HTML5 textarea attribute "form". It allows a textarea to be part of a specified form without being inside the form element. http://www.w3schools.com/tags/att_textarea_form.asp
也许您的审阅者指的是 HTML5 textarea 属性“form”。它允许 textarea 成为指定表单的一部分,而无需位于表单元素内。 http://www.w3schools.com/tags/att_textarea_form.asp
But generally speaking, as long as you can identify an element, say a textarea, you can access the text inside it and send it to the server without submitting any forms using ajax.
From Sable's comment:
http://api.jquery.com/jquery.postOR
http://api.jquery.com/jquery.ajax/
但一般来说,只要你能识别一个元素,比如一个 textarea,你就可以访问其中的文本并将其发送到服务器,而无需使用 ajax 提交任何表单。来自 Sable 的评论:
http: //api.jquery.com/jquery.post或
http://api.jquery.com/jquery.ajax/
回答by Justin
Yes, you can submit data to a server without putting it into a form
. Form's provide a simpler mechanism for sending larger amounts of data to a server without the developer having to tell the browser how to encode the form.
是的,您可以将数据提交到服务器,而无需将其放入form
. 表单提供了一种更简单的机制,可以将大量数据发送到服务器,而无需开发人员告诉浏览器如何对表单进行编码。
EG:
例如:
HTML
HTML
JS
JS
var text = $("input[type='text']").val();
$.post("/my/server/url", { userInput : text }, function(data){ /* Success! */});
This would technically post the data to the server without a form.
从技术上讲,这将在没有表单的情况下将数据发布到服务器。