2013-01-07 44 views
3

可能重复:
How to encode a URL in JavaScript?如何使用Javascript ajax发送网址?

我尝试使用下面的代码的PHP代码发送一个网址,但作为URL包括& A = 12 & B = 4 一旦我在我的php代码中获得了“a”变量的值,地址的最后一部分被删除。

URL = http://www.example.com/help.jpg?x=10&a=12&b=4 但我在我的PHP文件中获取URL是http://www.example.com/help.jpg?x=10(& A = 12 & B = 4被删除,我知道原因了JavaScript,Ajax和URL地址混合起来,做不知道它只是一个值,但不知道如何解决它)

  function upload(url){ 

      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) 
       { 
        document.getElementById("output").innerHTML= xmlhttp.responseText; 
       } 
      } 
      xmlhttp.open("GET","Photos.php?a="+url,true); 
      xmlhttp.send(); 
    }   


    if(isset($_GET["a"])) 
    { 
     $Address = $_GET["a"]; 
     echo $Address; 

    } 

输出>>>“http://www.example.com/help.jpg?x=10”,但它应该是http://www.example.com/help.jpg?x=10&a=12&b=4

+1

检查了这一点:http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript –

+1

密切相关:http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript – gd1

+1

@ gd1哇,这是一个有趣的巧合。 –

回答

4

您需要的参数编码

xmlhttp.open("GET","Photos.php?a="+encodeURIComponent(url),true); 
0

您需要对URL进行编码。您可以使用encodeURIComponent方法(STR)与encodeURI(STR),如:

var eurl = encodeURIComponent("http://www.example.com/help.jpg?x=10&a=12&b=4"); 
xmlhttp.open("GET","Photos.php?a=" + eurl, true);