2015-11-19 73 views
1

具體指標在我的代碼執行一個Ajax請求,並將其發送內容:

Object {appointment_data: "{"id_services":["13","13"],"id_users_provider":"86…lable":false,"id":"133","id_users_customer":"87"}", customer_data: "{"first_name":"Mario","last_name":"Rossi","email":…:"","city":"","zip_code":"","notes":"","id":"87"}"} 
appointment_data: "{"id_services":["13","13"],"id_users_provider":"86","start_datetime":"2015-11-19 13:43:00","end_datetime":"2015-11-19 14:55:00","notes":"","is_unavailable":false,"id":"133","id_users_customer":"87"}" 
customer_data: "{"first_name":"Mario","last_name":"Rossi","email":"[email protected]","phone_number":"0000","address":"","city":"","zip_code":"","notes":"","id":"87"}" 

注:此內容包含在appointment變量是JSON編碼:

JSON.stringify(appointment); 

現在從PHP端,被調用的函數裏面,我試圖讓這種方式任命的id

$_POST['appointment_data']['id']; 

但我得到這個錯誤:

Illegal string offset 'id'

我也試圖與.id但同樣出現。

注意:如果我執行gettype()我得到字符串$_POST['appointment_data'] 也許這是問題嗎?我如何解決這個問題?

VAR DUMP PRINT

array(2) { ["appointment_data"]=> string(216) "{"id_services":["13","15","14"],"id_users_provider":"86","start_datetime":"2015-11-19 09:45:00","end_datetime":"2015-11-19 10:57:00","notes":"Appuntamento ","is_unavailable":false,"id":"131","id_users_customer":"87"}" ["customer_data"]=> string(146) "{"first_name":"Mario","last_name":"Rossi","email":"[email protected]","phone_number":"0000","address":"","city":"","zip_code":"","notes":"","id":"87"}" } 
+0

嘗試做'$ _POST ['appointment_data'] - > id;'(懷疑它會工作,但值得一試!) –

+0

你不需要解析/解碼嗎? – FirstOne

+0

與山姆提示我得到:消息:試圖獲得非物件的財產 我應該解析它? – Dillinger

回答

3

PHP是不會自動將您的JSON字符串轉換爲對象。 HTTP POST和PHP的組合並不那麼直觀,並且可能不應該嘗試。你最大的問題線索就是你在這裏的講話:

if I execute gettype() I get string on $_POST['appointment_data']

在這種情況下,它是一個字符串,字符串沒有一個id指數。如果您想要在JSON字符串轉換爲對象,PHP提供了一種方法來做到這一點:

$myObj = json_decode($_POST['appointment_data']); 

在這一點上,你要尋找的值應該是可供選擇:

$myObj->{'id'} 
+0

這工作很好,謝謝你。祝你有美好的一天 :) – Dillinger