How can I send a JSON response from a Perl CGI program?

前端 未结 3 1832
时光说笑
时光说笑 2020-12-15 18:55

I am writing a JSON response from a perl/cgi program. The header\'s content type needs to be \"application/json\". But it doesn\'t seems to be recognized as response is thro

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 19:18

    Even if you specify the type "application/json" you still need to parse the text. jQuery do this for you, using the $.getJSON function,ie:

    $.getJSON("http://someurl.com/blabla.json",{some: "info"},function(json){
      alert(json["aKey"]["anotherOne"]);
    });
    

    (here the specs).

    But maybe you are already aware of this, so the problem resides somewhere else: can you please tell us a sample of your json response, because maybe the problem is that is not valid. It's not really clear to me why you say that "doesnt seems to be recognised": when I write json services the first test I do is to call them on the browser and maybe fire up firebug and try to parse it (so yes the response it's a text response, but javascript it's still quite happy to parse it and return a json object).

提交回复
热议问题