Javascript ajax与许多参数发送到PHP文件


javascript ajax with many parameters sent to php file

在我的HTML文件中,末尾有以下代码:

<script type="text/javascript">
  function voteUp(userID,userName,channelID,messageID,voteUp,voteDown)
  {
    $.get("_vote/vote_ajax.php?userID="+userID+"&userName="+userName+"&channelID="+channelID+"&messageID="+messageID+"&voteUp="+voteUp+"&voteDown="+voteDown, function(response){
       // alert("Data: " + data + "'nStatus: " + status);
       alert(response);
    });
  } 
</script>

但是我有错误,当我加载HTML页面:

XML解析错误:not well-formed位置:http://localhost/ajaxChat/第626行,第55列$ . get (" _vote/vote_ajax.php ?用户id = " +用户名+",用户名= " +用户名+ "和channelID = " + channelID +消息id ",消息id = " + + "和voteUp = " + voteUp +",voteDown = " + voteDown函数(响应){-------------------------------------------------------------^

如果我只使用一个参数,则HTML页面正在正确加载:

<script type="text/javascript">
  function voteUp(userID,userName,channelID,messageID,voteUp,voteDown)
  {
    $.get("_vote/vote_ajax.php?userID="+userID, function(response){
       // alert("Data: " + data + "'nStatus: " + status);
       alert(response);
    });
  } 
</script>

您的页面正在通过XML解析器运行,因此看起来您需要添加一个CDATA块

<script type="text/javascript">
/* <![CDATA[ */
  function voteUp(userID,userName,channelID,messageID,voteUp,voteDown) {
    $.get("_vote/vote_ajax.php?userID="+userID+"&userName="+userName+"&channelID="+channelID+"&messageID="+messageID+"&voteUp="+voteUp+"&voteDown="+voteDown, function(response){
       // alert("Data: " + data + "'nStatus: " + status);
       alert(response);
    });
  } 
/* ]]> */
</script>

好吧,我在Blogger模板中使用带有多个参数的链接时遇到过这个问题,例如,它们要经过XML解析器。

你要做的就是把"&"替换为"&amp;"

就是这样!

 $.get("_vote/vote_ajax.php?userID="+userID+"&amp;userName="+userName+"&amp;channelID="+channelID+"&amp;messageID="+messageID+"&amp;voteUp="+voteUp+"&amp;voteDown="+voteDown, function(response){ ...