解码来自Ajax Call的Json响应

时间:2013-12-10 13:12:55

标签: javascript jquery python ajax json

我通过我的html页面上的ajax调用从python脚本获得json响应,我在localhost上运行。当我警告 /显示响应时,它处于正确的ajax格式,但我不知道如何解码它。 JSON解析显示 [object] [object] 。有帮助吗?提前谢谢。

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()

4 个答案:

答案 0 :(得分:2)

你应该使用

console.log(data)

而非警报(数据)。警报只会显示字符串

答案 1 :(得分:1)

alert()仅输出字符串。所以一般来说它会是:

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

将console.log或console.dir与JSON.parse结合使用,以显示从JSON.parse返回的实际对象。

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

您还可以临时将其设置为全局变量,以通过键入其名称来通过Firebug / Chrome DevTools控制台对其进行调试。例如:

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

然后在控制台中输入“data”。

请注意,这是运送到生产的不良做法,全局变量不能被浏览器JavaScript引擎垃圾收集,特别是全局变量名称,因为数据很可能与其他全局变量发生冲突。如果由于某种原因仍想使用全局,请确保使用可靠的命名约定来防止错误。

答案 2 :(得分:0)

您可以使用$ .parseJSON正确格式化数据。您可能还希望在错误响应函数结束时删除额外的逗号。它可能导致语法错误。从“action:'muawia()',”中删除逗号。

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

希望这有帮助!

答案 3 :(得分:0)

JSON.parse(stringData)创建普通的JavaScript对象时,这是绝对正常的,因此您可以在警报中获得[object Object]。您必须访问每个对象的属性才能获取其值。在你的情况下     var jsonObj = JSON.parse(data); alert(jsonObj.access.token.issued_at);

将是例如“2013-04-18T14:40:23.299903”