简体   繁体   中英

Javascript - sending html code in string via Ajax

How is the best way to send string from javascript to php with html tags inside? I'm trying, but all html tags disappear.

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); shows string like that: 'some text' instead

'<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).

try to escape it before using 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 ? I suspect your browser is actually parsing the div and b tags, so the HTML is not showing up in the text...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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