2013-07-01 58 views
1

我創建了一個使用瓶子的openerp API,並且工作正常。Python Bottle API使用外部API訪問時沒有Json響應

每當我訪問使用瀏覽器

例如:http://example.com/api/user_name=uname&password=pwd

返回JSON值,也同時使用python

訪問相同的API響應但問題是,當我使用外部API訪問PHP它不響應或返回json數據。

這裏是我的wgsi代碼

from bottle import Bottle,get,post,run,request,error,route,template,validate,debug,response 
    import json 
    import os 
    import sys 
    import bottle 
    @route ('/user_name=:user_name&password=:password', method='GET') 
    @route ('/user_name=:user_name&password=:password', method='POST') 
    def login_validate(user_name,password): 
     import xmlrpclib 
     print "test" 
     dbname = 'more' 
     sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common') 
     uid = sock_common.login(dbname, user_name, password) 
     if uid: 
      sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object') 
      adv_fields = ['name'] 
      adv_args=[('user_id','=',uid)] 
      adv_id = sock.execute(dbname, uid, password, 'res.partner', 'search', adv_args) 
      if adv_id: 
       res = sock.execute(dbname, uid, password, 'res.partner', 'read',adv_id, adv_fields) [0] 
       print res,type(res) 
       return json.dumps({'Sucesss':res['name']}) 
      else: 
       return json.dumps({'Error':'User Found but not a partner'}) 
     else: 
      return json.dumps({'Failure':'Invalid User Name or Password'}) 
    application = bottle.default_app() 
+0

你可以張貼一些調試信息?參數是否正確發送?某些'print(user_name,password)'可能有幫助。 –

+0

訪問使用PHP內容類型不返回作爲JSON它返回作爲gzip – senthilnathang

+0

你到底如何部署此應用程序?涉及哪個Web服務器?似乎gzip發生在wsgi之後,即:nginx,apache .. –

回答

1

第一選擇:禁用的Apache gzip壓縮

Include mods-enabled/*.loadInclude mods-enabled/*.conf,檢查mods-enabled文件夾內,看看是否有任何引用到mod_deflate模塊(Apache的gzip的comrpession )。

如果你是一個Debian系統一樣你可能會與此禁用它:

a2dismod deflate 
service apache2 restart 

如果你在RedHat/Centos的再檢查/etc/httpd/conf/httpd.conf和編輯LoadModule線。

第二選擇:讀gzip的響應

Uncompress gzip compressed http response

+0

其實問題是與PHP開發人員的JSON他使用'JSON',而不是'JSONp'。我檢查他的代碼後,我發現問題是他方檢查這一個細節http://stackoverflow.com/questions/2887209/what-are-the-differences-between-json-and-jsonp – senthilnathang