简体   繁体   English

Javascript-通过Ajax以字符串形式发送html代码

[英]Javascript - sending html code in string via Ajax

How is the best way to send string from javascript to php with html tags inside? 如何将带有html标签的字符串从javascript发送到php的最佳方法? I'm trying, but all html tags disappear. 我正在尝试,但是所有html标签都消失了。

var ajaxData = '<div>some <b>text</b></div>';

jQuery.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            data: ajaxData,
});

but in my php code var_dump($input); 但是在我的PHP代码中var_dump($ input); shows string like that: 'some text' instead 显示类似这样的字符串:“ some text”

'<div>some <b>text</b></div>'

even this doesn't work 即使这不起作用

htmlspecialchars(urldecode($input));

did you tried to view source? 您是否尝试查看源代码? var_dump will output the variable as is, so if it contains HTML the browser will parse it and you won't see the HTML part (only in view source). var_dump将按原样输出变量,因此如果它包含HTML,浏览器将对其进行解析,并且您将看不到HTML部分(仅在视图源中)。

try to escape it before using var_dump. 尝试在使用var_dump之前对其进行转义。

var_dump(htmlspecialchars($input));

Can you take a look at the page source for the page where you're doing the var_dump ? 您可以看看正在执行var_dump的页面的页面源吗? I suspect your browser is actually parsing the div and b tags, so the HTML is not showing up in the text... 我怀疑您的浏览器实际上是在解析divb标签,因此HTML不会显示在文本中...

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

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