對不起英文:)張貼在json格式
我試圖張貼形式以json格式爲在PHP/python書面restapi。如果我使用json,我無法訪問發佈的數據。見下面
的情況下非JSON的代碼發佈
jQuery(document).ready(function($) {
$.post(
"http://localhost:8000/api/v1/entry/?format=json",
{
"body": "This will prbbly be my lst edited post.",
"pub_date": "2011-05-22T00:46:38",
"slug": "another-post",
"title": "Another Post",
"username":"admin",
"password":"admin"
},
function(responseText){
$("#result").html(responseText);
},
"html"
);
})
服務器響應
Array
(
[body] => This will prbbly be my lst edited post.
[pub_date] => 2011-05-22T00:46:38
[slug] => another-post
[title] => Another Post
[username] => admin
[password] => admin
)
代碼對JSON後
jQuery(document).ready(function($) {
var data = JSON.stringify({
"body": "This will prbbly be my lst edited post.",
"pub_date": "2011-05-22T00:46:38",
"slug": "another-post",
"title": "Another Post",
"username":"admin",
"password":"admin"
});
$.post(
"testpost.php",
data,
function(responseText){
$("#result").html(responseText);
},
"html"
);
})
服務器響應
Array
(
)
的eval(數據)會做什麼?或者它會簡單地將json轉換回對象?只是越來越好奇:) – sumit
在PHP或JavaScript?在PHP中,使用'json_decode'並在JavaScript中使用'JSON.parse'。 –