duanlu0386 2019-04-18 09:29
浏览 45

如何修复帖子通过java发送%ad和php接收?

O have page with some javascript code and I sending data from textarea to php file save.php to save data on server

code:

var data = 'data='+document.getElementById("data").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState==4 && xmlhttp.status==200){
    response.innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("POST","save.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(data);

send data is okey but if I echo data in php on save.php it's broken %ad -> �

save.php code:

$post_data = $_POST['data'];
echo $post_data;

textarea contains:

somethink like:

%packages --excludedocs
@^minimal
@core
kexec-tools
%end

%addon com_redhat_kdump --enable --reserve-mb='auto'
%end

with %addon is problem becouse it changed to �don com_redhat_kdump.....

  • 写回答

2条回答 默认 最新

  • duanfei1987 2019-04-18 11:47
    关注

    That � is a special character which means an invalid character code was used. This is a sign that you have used an invalid encoding.

    I would suggest you set the character encoding of your request in javascript.

    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=utf-8");
    

    Also don't echo the data directly into a web page from php without encoding it in HTML.

    echo htmlentities($post_data, ENT_HTML5, 'UTF-8');
    

    You can also look at the web request headers using your browsers development tools to verify that the server isn't sending the web page using a different character encoding. If it is you can set the header in PHP.

    header('Content-Type: text/html; charset=utf-8');
    
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了