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

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

问题描述

我正在使用 jQuery 使用 div 的 HTML 内容向 PHP 文件发送 POST.HTML 内容包含表格、输入、较小的 div,我想获取主 DIV 的内容并将其发送到数据库.我能想到的唯一选择是 POST 方法,但我不知道是否可以用它发送纯 HTML.是否还有其他选项可以将 HTML 内容从 div 发送到要插入 MySQL 的 PHP 文件?

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 字符会转换为特殊字符.示例:>"会变成>"当这种情况发生时,我的 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.

有什么解决方法吗?

再次感谢.

推荐答案

就您而言,一旦您使用 .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>

警报文本是字符串.只要您不将它传递给解析器,它就没有任何神奇之处,它就像任何其他字符串一样是一个字符串.
没有什么可以阻止您使用 .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.

edit:不要将字符串作为参数 data 传递给 .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天全站免登陆