duanbinren8906 2014-01-25 20:02
浏览 107
已采纳

Ajax调用返回[object HTMLInputElement]

i am learning ajax and doing some practice. I am facing a problem. Here is my code.

<input class="category" id="design" type="button" value="Design" onclick="loadXMLDoc(design)" />

Ajax:

function loadXMLDoc(name)
{
 var xmlhttp;
 if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
    xmlhttp.onreadystatechange=function()
   {
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {

    var array = xmlhttp.responseText;
    alert(array);


     }
   }
  xmlhttp.open("GET","server.php?cat="+name,true);
  xmlhttp.send();
 }

server.php:

if(isset($_GET['cat']))
{
    $cat = $_GET['cat'];
    echo $cat;


}

Now, when i click on the button, the alter gives me [object HTMLInputElement] when i am expecting to get "design". What is wrong in it?

  • 写回答

2条回答 默认 最新

  • douque9815 2014-01-25 20:05
    关注

    What is wrong in it?

    You have to pass a string to the function:

    onclick="loadXMLDoc('design')"
    

    Currently you are passing the variable design. Since you have an element with ID "design" this variable happens to refer to that element. Then when you are trying to send the element to the server it is converted to a string. The default string representation of an input DOM element in JavaScript is "[object HTMLInputElement]".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料