2011-05-19 73 views
0

我正在使用Ajax发布内容,并且我使用http.send(encodeURIComponent(params));进行编码...但我无法在PHP中解码它们。我使用POST,所以我不认为它需要编码?我很困惑,我应该如何在PHP中值解码...decodeURIComponent(uri)不工作?

params = "guid="+szguid+"&username="+szusername+"&password="+szpassword+"&ip="+ip+"&name="+name+"&os="+os; 
     //alert(params); 
     document.body.style.cursor = 'wait';//change cursor to wait 

     if(!http) 
      http = CreateObject(); 

     nocache = Math.random(); 

     http.open('post', 'addvm.php'); 
     http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     http.setRequestHeader("Content-length", params.length); 
     http.setRequestHeader("Connection", "close"); 
     http.onreadystatechange = SaveReply; 
     http.send(encodeURIComponent(params)); 

回答

3

encodeURIComponent将编码分隔键/值对所有& S和= S。您需要分别在每个部件上使用它。像这样:

params = 
"guid="  + encodeURIComponent(szguid)  + 
"&username=" + encodeURIComponent(szusername) + 
"&password=" + encodeURIComponent(szpassword) + 
"&ip="  + encodeURIComponent(ip)   + 
"&name="  + encodeURIComponent(name)  + 
"&os="  + encodeURIComponent(os); 
    //alert(params); 
    document.body.style.cursor = 'wait';//change cursor to wait 

    if(!http) 
     http = CreateObject(); 

    nocache = Math.random(); 

    http.open('post', 'addvm.php'); 
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    http.setRequestHeader("Content-length", params.length); 
    http.setRequestHeader("Connection", "close"); 
    http.onreadystatechange = SaveReply; 
    http.send(params); 
+0

不,你不会。你想在JavaScript中做到这一点。在PHP中做这件事是不必要的困难和毫无意义的。 – Ryan 2011-05-19 03:43:59

+0

@minitech:okies :)但如何?就像我使用$ username = trim($ _ POST [“username”]); $ password = $ _POST [“password”]; $ ip = trim($ _ POST [“ip”]); $ name = trim($ _ POST [“name”]); $ os = trim($ _ POST [“os”]); $ hostid =“物理”;在PHP如何解码他们...之后,我所有的领域能够采取+和&字符串? – Harsh 2011-05-19 03:46:52

+0

@minitech:我想解码他们在PHP? – Harsh 2011-05-19 03:50:37

1

如果你在PHP提交从JS编码值,并希望他们decode你可以这样做:

// decode POST values so you don't have to decode each pieces one by one 
$_POST = array_map(function($param) { 
      return urldecode($param); 
     }, $_POST); 

// assign post values after every value is decoded 
+0

我可以使用$ _GET(将上述脚本的所有$ _POST替换为$ _GET)? – bungdito 2011-08-23 05:33:34

+0

@bungdito codewise是的,但它不会是一个好的做法。我也没有看到你需要这样做的原因。 – tradyblix 2011-08-23 05:40:35