Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果调用函数时提供的参数包含空格,则不调用函数_Javascript_Php_Last.fm - Fatal编程技术网

Javascript 如果调用函数时提供的参数包含空格,则不调用函数

Javascript 如果调用函数时提供的参数包含空格,则不调用函数,javascript,php,last.fm,Javascript,Php,Last.fm,我正在使用last.fm api。我有一个JavaScript AJAX函数,如下所示 function showart(art) { if (art=="") { document.getElementById("info").innerHTML=""; return; } if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xm

我正在使用last.fm api。我有一个JavaScript AJAX函数,如下所示

function showart(art) {
    if (art=="") {
    document.getElementById("info").innerHTML="";
    return;
    } 
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("info").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","5.php?a="+art,true);
    xmlhttp.send();
}
这是我调用它的PHP代码

for($i=1;$i<=10;$i++) {
    echo '<table><td width="36px">';
    echo '<img src="'.$info->topartists->artist[$i-1]->image.'"></td>';
    echo '<td><a class="artist" href=# onclick=showart(\''.$info->topartists->artist[$i-1]->name.'\')>'.$info->topartists->artist[$i-1]->name.'</a></td></table>';
}

对于($i=1;$i您必须对参数值进行编码:

xmlhttp.open("GET","5.php?a=" + encodeURIComponent(art), true);
这将正确地对空间进行编码,服务器将对其进行解码

编辑正在创建
”的代码;

注意
showart()周围添加的
引号
call.

除非您支持IE6,否则ActiveX调用是不必要的,不管它值多少钱。它不起作用,我想提供的参数也应该被编码或其他东西。@VermaJr。您怎么知道它不起作用?您在开发人员控制台中检查了错误吗?您检查了控制台发出的HTTP请求了吗?您在服务器上做过任何调试吗?
encodeURIComponent()
调用是绝对必要的,但可能有许多其他错误。当我单击名为“Coldplay”的链接时,结果会显示,而当我单击“Linkin Park”时,结果不会显示。您可以在此处查看:如果您检查浏览器控制台,您会看到报告了JavaScript错误。代码为在ajax尝试之前就被破坏了。是的,那么如何修复它呢?
echo '<td><a class="artist" href=# onclick="showart(\''.$info->topartists->artist[$i-1]->name.'\')">'.$info->topartists->artist[$i-1]->name.'</a></td></table>';