2011-05-19 50 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