2013-09-10 66 views
1

所有,不能json_decode GET參數

我想傳遞一個字符串化JSON對象作爲一個GET參數,但接收URL似乎無法對其進行解碼,我不知道爲什麼。

下面是相關代碼:

客戶端的JSON對象生產(工作正常):

function createJson(){ 
// This works fine. It creates a json objects with three elements, the third being an array. 
//(omitted code for brevity) 
    return jsonData; 
} 

客戶端AJAX調用(正常工作):

function recordSetGet(jsonData){ 
    request = createRequest(); 
    var rawSet=JSON.stringify(jsonData); 
    var encodedSet=encodeURIComponent(rawSet); 
    var params="set="+encodedSet; 
    var url= "Ajax_recordSetGet.php?"+params; 
    request.open("GET", url, true); 
    request.onreadystatechange = function(){} 
    request.send(null); 
} 

這產生以下網址:Ajax_recordSetGet.php?set=%7B%22setTitle%22%3A%22test%22%2C%22setTags%22%3A%22test%20%22%2C%22set%22%3A%5B%7B%22first%22%3A%22Joe%22%2C%22last%22%3A%22Doe%22%2C%22checked%22%3Atrue%7D%5D%7D"

服務器端處理:

<?php 
header('Content-Type:text/html; charset=UTF-8'); 
if(!session_id()){ 
    session_start(); 
} 
if(isset($_GET['set'])){ 
    $set=$_GET['set'];//This is the URI encoded string 
    var_dump ($set); 
    var_dump (json_decode($set)); 
    var_dump ("Json last error is ".json_last_error()); 
} 
?> 

的var_dumps的結果是: string '{"setTitle":"test","setTags":"test ","set":[{"first":"Joe","last":"Doe","checked":true}]}"' (length=90)
null
string 'Json last error is 4' (length=20)

那麼,爲什麼json_decode()這裏失敗? json_last_error()結果表明語法錯誤。

編輯:
如果我建立帕拉姆字符串沒有這樣的編碼是:需要注意的是json_decode如果我發送一個非編碼字符串也沒有

var rawSet=JSON.stringify(jsonData); 
var params="set="+rawSet; 
var url= "Ajax_recordSetGet.php?"+params; 

然後URL成爲Ajax_recordSetGet.php?set={"setTitle":"test","setTags":"test ","set":[{"first":"Joe","last":"Doe","checked":true}]}"

並且接收URL var_dump產生相同的錯誤:
string '{"setTitle":"test","setTags":"test ","set":[{"first":"Joe","last":"Doe","checked":true}]}"' (length=90)
null
string 'Json last error is 4' (length=20)

+0

請結識[The SSCCE](http://sscce.org/)。 –

+1

** TL; DR **。嘗試讓你的問題更簡潔。如果我們需要更多,我們會問。 –

+0

這是很多我沒看過的代碼。 –

回答

1

我不知道爲什麼,但你必須在你的URL尾隨雙引號是攪亂JSON解碼過程。

...%7D%5D%7D" <- Trailing double quote 

這句話也顯示在您設定的變量的轉儲:

...true}]}"' (length=90) <- double quote is inside the string, the single quote is part of the var_dump output. 

這雙引號必須在JavaScript代碼推出,雖然我不能看到一個地方,那應該發生。但是你的調試應該在系統的那個部分進行。

請注意,您不得將urldecode作爲「set」參數傳遞,因爲PHP已經爲您完成了解碼。但隨着尾部雙引號,你不能撥打json_decode()

作爲一個概念的快速證明,請在解碼之前在您的PHP代碼中使用$set = rtrim($set, '"')以刪除該引號字符。

+0

在第一個雙引號之前是反斜槓。 –

+0

究竟在哪裏? – Sven

+0

你說的沒錯,在這個URL的末尾有一個雙引號。當我手動刪除它時,json_decode工作正常。 – JDelage

1

http://php.net/manual/en/function.urldecode.php

The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.

嘗試用不同的方式。我建議使用base64編碼json字符串。

取而代之的是:

var rawSet=JSON.stringify(jsonData); 
var encodedSet=encodeURIComponent(rawSet); 
var params="set="+encodedSet; 
var url= "Ajax_recordSetGet.php?"+params; 

做到這一點:

var rawSet=JSON.stringify(jsonData); 
var encodedSet=btoa(rawSet); 
var params="set="+encodedSet; 
var url= "Ajax_recordSetGet.php?"+params; 

但要注意的caveats

而在你的服務器端:

$decodedSet=json_decode(base64_decode($set)); 
+0

爲什麼?另外,請注意,我在原始'$ _GET'參數和解碼版本上都嘗試'json_decode'。既沒有工作。 – JDelage

+0

這是在php中的常見投訴。 Php就是這樣實現的。我不喜歡它,但它是一個黃蜂巢。 –

+0

Php正在破壞你的urlencoded獲取參數。這是沒有辦法的。所以使用不同的編碼機制。這是我答案的核心。 –