【问题标题】:javascript ajax with many parameters sent to php file带有许多参数的javascript ajax发送到php文件
【发布时间】:2015-12-26 00:13:50
【问题描述】:

在我的 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 解析错误:格式不正确 位置:http://localhost/ajaxChat/ 第 626 行,第 55 列: $.get("_vote/vote_ajax.php?userID="+userID+"&userName="+userName+"&channelID="+channelID+"&messageID="+messageID+"&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 解析器运行 HTML 页面?你真的在使用 XHTML 吗?
  • console.log(userName),检索什么?

标签: javascript php ajax


【解决方案1】:

您的页面正在通过 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>

【讨论】:

  • 它正在工作!非常感谢您的快速回答。
【解决方案2】:

好的,我在 Blogger 模板中使用具有多个参数的链接时遇到了这个问题,例如,它们在哪里通过 XML 解析器。

您需要做的是替换 "&amp;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){ ...

【讨论】:

    猜你喜欢
    • 2016-09-04
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多