2013-12-10 25 views
1

我在我的html頁面上通過ajax調用從python腳本獲取json響應,該響應在本地主機上運行。當我警報 /顯示響應它是在適當的ajax格式,但我不知道如何解碼它。 JSON解析顯示[對象] [對象]。任何幫助?提前致謝。解碼JSON來自Ajax調用的響應

HTML:

function getData() { 
    // Code doesn't even enter this function but when i remove the $.ajax part it enters the function 
    alert("I AM HERE"); 

    $.ajax({ 
     type: "GET", 
     datatype: 'json', 
     url: "/cgi-bin/check.py", 
     data: { 
      action: 'muawia()', 
     }, 
     success: function(data) { 
      alert(data); 
     }, 
     error: function(data) { 
      alert(data.responseText); 
     } 
    }); 
}; 

的Python:

#!/usr/bin/python 

import cgi, cgitb 
from StringIO import StringIO 
import json 

class myclass: 
    def __init__(self): 
      self.data = [] 


    def muawia(self): 
     content=json.loads('{"access": {"token": {"issued_at": "2013-04-18T14:40:23.299903", "expires": "2013-04-19T14:40:23Z", "id": "4c5ef01f52c7404fb5324c520d25d1fe", "tenant": {"description": "admin tenant", "enabled": true, "id": "51ad87714b86442d9a74537d6f890060", "name": "admin"}}, "serviceCatalog": [{"endpoints": [{"adminURL": "http://10.199.0.250:8774/v2/51ad87714b86442d9a74537d6f890060", "region": "RegionOne", "internalURL": "http://10.199.0.250:8774/v2/51ad87714b86442d9a74537d6f890060", "id": "9869f55f0de2490685676b6ec27f6097", "publicURL": "http://10.199.0.250:8774/v2/51ad87714b86442d9a74537d6f890060"}], "endpoints_links": [], "type": "compute", "name": "nova"}, {"endpoints": [{"adminURL": "http://10.199.0.250:8080", "region": "RegionOne", "internalURL": "http://10.199.0.250:8080", "id": "321601d827ba4bbbb6de1df69fd43a1c", "publicURL": "http://10.199.0.250:8080"}], "endpoints_links": [], "type": "s3", "name": "swift_s3"}, {"endpoints": [{"adminURL": "http://10.199.0.250:9292", "region": "RegionOne", "internalURL": "http://10.199.0.250:9292", "id": "cca7d7a24dbe45b6ae08da2c023b0d82", "publicURL": "http://10.199.0.250:9292"}], "endpoints_links": [], "type": "image", "name": "glance"}, {"endpoints": [{"adminURL": "http://10.199.0.250:8776/v1/51ad87714b86442d9a74537d6f890060", "region": "RegionOne", "internalURL": "http://10.199.0.250:8776/v1/51ad87714b86442d9a74537d6f890060", "id": "14773153229d4e7f80e47cf7b1dd2d15", "publicURL": "http://10.199.0.250:8776/v1/51ad87714b86442d9a74537d6f890060"}], "endpoints_links": [], "type": "volume", "name": "cinder"}, {"endpoints": [{"adminURL": "http://10.199.0.250:8773/services/Admin", "region": "RegionOne", "internalURL": "http://10.199.0.250:8773/services/Cloud", "id": "064df72a67f54dffa68c07b8fc400bdb", "publicURL": "http://10.199.0.250:8773/services/Cloud"}], "endpoints_links": [], "type": "ec2", "name": "nova_ec2"}, {"endpoints": [{"adminURL": "http://10.199.0.250:8080/", "region": "RegionOne", "internalURL": "http://10.199.0.250:8080/v1/AUTH_51ad87714b86442d9a74537d6f890060", "id": "194df182a8c043e48175a40fb615064e", "publicURL": "http://10.199.0.250:8080/v1/AUTH_51ad87714b86442d9a74537d6f890060"}], "endpoints_links": [], "type": "object-store", "name": "swift"}, {"endpoints": [{"adminURL": "http://10.199.0.250:35357/v2.0", "region": "RegionOne", "internalURL": "http://10.199.0.250:5000/v2.0", "id": "34db74b5f32f4121932725b1146a1701", "publicURL": "http://10.199.0.250:5000/v2.0"}], "endpoints_links": [], "type": "identity", "name": "keystone"}], "user": {"username": "admin", "roles_links": [], "id": "b5902682120742baa150945d8a37ff47", "roles": [{"name": "admin"}], "name": "admin"}, "metadata": {"is_admin": 0, "roles": ["9aa2eb385f4e4a8e80ad5002c212e76b"]}}}') 
     data=json.dumps(content, indent=4, separators = (', ', ': ')) 
     print data 
     return 

print "Content-Type: text/html\n" 
x = myclass() 
x.muawia() 
+0

你應該返回一個** application/json的** Content-Type **也不要使用CGI!使用真正的Python Web框架。 –

+1

你嘗試過stringify嗎? http://stackoverflow.com/questions/4810841/json-pretty-print-using-javascript –

+0

你應該打印一個空行來分隔HTTP標頭和主體。 – SuperSaiyan

回答

0

您可以使用$ .parseJSON以正確格式的數據。您可能還想在錯誤響應函數結尾刪除額外的逗號。它可能會導致語法錯誤。從「action:'muawia()'」中移除逗號,以及。

success: function(data){ 
    r = $.parseJSON(data) 
    alert(r.responseText); 
}, 
error: function(data){ 
    r = $.parseJSON(data) 
    alert(r.responseText); 
} 

希望這有助於!

0

這是絕對正常的,因爲JSON.parse(stringData)創建一個正常的JavaScript對象,所以你在[object Object]的警報。您必須訪問每個對象的屬性才能獲取其值。在你的情況 var jsonObj = JSON.parse(data); alert(jsonObj.access.token.issued_at);

將例如 「2013-04-18T14:40:23.299903」

2

您應該使用

console.log(data) 

,而不是警報(數據)。警報只會顯示字符串

1

alert()僅輸出字符串。所以通常這將是一樣的:在JSON.parse組合表明,從JSON.parse返回的實際對象

data = data.toString(); 
alert(data); 

使用的console.log或console.dir。

// In your $.ajax success method 
var someVar = JSON.parse(data); 
console.log(someVar); 

您還可以將其設置爲一個全局變量暫時通過螢火/ Chrome的DevTools控制檯通過鍵入其名稱來調試。例如:

// In your $.ajax success method 
window.data = data; 

然後在控制檯中輸入「data」。

請注意,這是發佈到生產環境的糟糕做法,您的瀏覽器JavaScript引擎無法收集全局變量,特別是全局變量名稱,因爲數據很可能會與其他全局變量發生衝突。如果您仍然因爲某種原因想要使用全局,那麼請確保使用可靠的命名約定來防止錯誤。