简体   繁体   English

使用AJAX不带html直接发送html5 textarea<form>

[英]Use AJAX to send html5 textarea directly without html <form>

Recently I am confused about whether it's possible to send input/textarea data directly without being included in html <form> .最近我对是否可以直接发送 input/textarea 数据而不包含在 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.我想在网页中,如果我们想从用户那里获取信息然后将文本发送到身份验证服务器,我们必须使用<form>而不管它以哪种方式提交。

But an anonymous reviewer of my paper claims that <html> can be bypassed by using an html5 tag "textarea" and JS AJAX post.但是我论文的匿名审稿人声称可以通过使用 html5 标签“textarea”和 JS AJAX 帖子绕过<html> While I did lots of experiments trying to implement his way but all failed.虽然我做了很多尝试实现他的方式的实验,但都失败了。

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_void”的代码将 AJAX 的 url 更改为 ServerSocket(由 Java 实现)。 The server was able to get the POST event, but it cannot read the "data" of AJAX.服务器能够获取 POST 事件,但无法读取 AJAX 的“数据”。 The following is the html code:以下是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 >).似乎套接字服务器无法从 AJAX 读取(但它可以从 <form> + <action> 读取)。 Is there any way to fix the reading issue?有没有办法解决阅读问题?

Thank you谢谢

Ajax (Asynchronous Javascript & XML) is a way to send data from client to the server asynchronously. Ajax(Asynchronous Javascript & XML)是一种将数据从客户端异步发送到服务器的方法。 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.为此,您必须编写代码以使用 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/请参阅此供您参考http://api.jquery.com/jquery.ajax/

Perhaps your reviewer was referring to the HTML5 textarea attribute "form".也许您的审阅者指的是 HTML5 textarea 属性“form”。 It allows a textarea to be part of a specified form without being inside the form element.它允许 textarea 成为指定表单的一部分,而无需位于表单元素内。 http://www.w3schools.com/tags/att_textarea_form.asp 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.但一般来说,只要你能识别一个元素,比如一个 textarea,你就可以访问其中的文本并将其发送到服务器,而无需使用 ajax 提交任何表单。 From Sable's comment:来自 Sable 的评论:
http://api.jquery.com/jquery.post OR http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.posthttp://api.jquery.com/jquery.ajax/

Yes, you can submit data to a server without putting it into a form .是的,您可以将数据提交到服务器而无需将其放入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.表单提供了一种更简单的机制,可以将大量数据发送到服务器,而无需开发人员告诉浏览器如何对表单进行编码。

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.从技术上讲,这将在没有表单的情况下将数据发布到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM