2013-10-13 31 views
0

JSON輸出有問題。我創建了一個AJAX後像這樣的:JSON輸出中的未知錯誤格式

function sign_up(form_data) 
{ 
     $.ajax({ 
     url: "signup_process.php", //target 
     data: {userdata:JSON.stringify(form_data)}, 
     type: "POST", //metode pengiriman 
     dataType: "json", //return data type 

    }).done(function(data) { 
     console.log(data); 
    }).fail(function(data, errorThrown, textStatus, jqXHR){ 
     console.log(textStatus); 

    }); 
} 

然後在服務器端處理它:

$data = json_decode($_POST['userdata'], true); 
echo json_encode($_POST['userdata']);die(); 

在我的本地服務器,它順利運行並顯示所需的輸出像這樣的:

{"email":"[email protected]","browser_agent":"chrome","browser_version":"30.0.1599.69","os":"win","device":"PC/Laptop/non-mobile-device","latitude":-6.211544,"longitude":106.84517199999999,"location":"Padang, Setiabudi, South Jakarta City, Jakarta 12850, Indonesia","ip":"139.xxx.xxx.xxx"} 

但是,當我上傳它在實時網絡服務器上,結果變成這樣(很多反斜槓)。我發現它打破了我的全部代碼:

{\"email\":\"[email protected]\",\"browser_agent\":\"chrome\",\"browser_version\":\"30.0.1599.69\",\"os\":\"win\",\"device\":\"PC/Laptop/non-mobile-device\",\"latitude\":-6.211544,\"longitude\":106.84517199999999,\"location\":\"Padang, Setiabudi, South Jakarta City, Jakarta 12850, Indonesia\",\"ip\":\"139.xxx.xxx.xxx\"} 

我真的不知道什麼是真正布萊恩因爲在我的本地代碼是與主機服務器上的代碼相同的100%。任何解決方案將非常感激。由於

+0

是否安全模式? – idmean

+0

不,我檢查過了,它已關閉。 – under5hell

回答

2

試試這個:

$data = json_decode(stripslashes($_POST['userdata']), true); 
echo json_encode($_POST['userdata']);die(); 

問題可能與magic quotes,或者你使用模擬這種效果(如WordPress)的系統。

stripslashes將從輸入中刪除斜槓。

+0

謝謝。你的建議真的幫助我解決這個問題。我意識到不僅僅是因爲我忘記了脫褲子,而是總是因爲我的陣列格式不好。謝謝 – under5hell