jQuery通过POST发送HTML数据 [英] jQuery send HTML data through POST

查看:167
本文介绍了jQuery通过POST发送HTML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery对具有HTML的HTML内容的PHP文件进行POST。 HTML内容包含表,输入,较小的div,我想抓取主DIV的内容并将其发送到数据库。我可以想到的唯一选择是POST方法,但我不知道是否可以发送纯HTML。有没有其他选项可以将HTML内容从div发送到PHP文件以插入到MySQL中?

I am using jQuery to make a POST to a PHP file with the HTML content of a div. The HTML content contain tables, inputs, smaller divs and I would like to grab the content of the main DIV and send it to the database. The only option I could think of is the POST method but I don't know if I can send plain HTML with it. Are there any other options for sending HTML content from a div to a PHP file to be inserted into MySQL ?

谢谢。

编辑:我现在可以使用jQuery的POST发送完整的HTML数据。但是,该DIV中的HTML字符会变成特殊字符。示例:>将成为& gt当发生这种情况时,我的POST数据被限制在第一个特殊字符出现的地方,因为我这样做我的ajax POST:

I am now able to send full HTML data with jQuery's POST. However, my HTML characters from that DIV gets transformed into special characters. Example: ">" would become ">" and when that happens, my POST data is limited to where the first special character appears because I do my ajax POST like this:

var data = 'id='+ currid +'&html='+ div_html;

    $.ajax({
          type: "POST",
          url: "file.php",
          data: data,
    .......................
    .......................

使用此代码不正确,因为div_html包含其他&字符,因此它将被视为另一个POST参数要发送。

Using this code is not ok because div_html contains other "&" characters so it would be considered as another POST parameter to be sent.

任何解决方法?

再次感谢

推荐答案

只要你用a href =http://docs.jquery.com/拉出内容属性/ htmlrel =noreferrer> html()它只是一个字符串,你可以用

As far as you're concerned once you've "pulled out" the contents with something like .html() it's just a string. You can test that with

<html> 
  <head>
    <title>runthis</title>
    <script type="text/javascript" language="javascript" src="jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready( function() {
            var x = $("#foo").html();
            alert( typeof(x) );
        });
    </script>
    </head>
    <body>
        <div id="foo"><table><tr><td>x</td></tr></table><span>xyz</span></div>
    </body>
</html>

警报文字是 string 。只要你不把它传递给一个解析器就没有任何魔法,它就像一个字符串,像任何其他的字符串。

没有什么阻碍你使用。post()将此字符串发送回服务器。

The alert text is string. As long as you don't pass it to a parser there's no magic about it, it's a string like any other string.
There's nothing that hinders you from using .post() to send this string back to the server.

< b>编辑:不要将参数数据的字符串传递给.post(),而是将对象(如

edit: Don't pass a string as the parameter data to .post() but an object, like

var data = {
  id: currid,
  html: div_html
};
$.post("http://...", data, ...);

jquery将处理参数的编码。

如果你原因)想保留你的字符串,你必须用像 escape()这样的值进行编码。

jquery will handle the encoding of the parameters.
If you (for whatever reason) want to keep your string you have to encode the values with something like escape().

var data = 'id='+ escape(currid) +'&html='+ escape(div_html);

这篇关于jQuery通过POST发送HTML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆