通过AJAX发送HTML服务器端PHP

通过AJAX发送HTML服务器端PHP

问题描述:

我试图发送HTML是一个形式到服务器端脚本,所以我可以将它保存在数据库中。我要送一个查询字符串,每次在HTML中包含像一个逗号或符号字符,在HTML的其余部分被截断在这一点上。

I'm trying to send HTML that is in a form to a server-side script so I can save it in the database. What I'm sending is a query string, and every time the HTML contains a character like a comma or ampersand, the rest of the HTML gets truncated at that point.

在此先感谢!

发送请求时,你应该适当的URL连接code parameteres:

When sending a request you should properly URL encode parameteres:

$.ajax({
    url: 'foo.php',
    data: { html: '<html>You can use whatever characters you want here</html>' },
    type: 'GET',
    success: function(result) {

    }
});

$.ajax({
    url: 'foo.php',
    data: { html: $('#someTextFieldWhichMightContainHtml').val() },
    type: 'GET',
    success: function(result) {

    }
});

现在你可以放心地阅读 HTML 变量在你的PHP脚本: $ GET [HTML]

Now you can safely read the html variable in your PHP script: $.GET["html"].

我想,现在的code看起来是这样的:

I suppose that right now your code looks something like this:

$.ajax({
    url: 'foo.php?html=' + $('#someTextField').val(),
    type: 'GET',
    success: function(result) {

    }
});

我会建议您不要使用字符串连接,并始终使用数据散。