【问题标题】:jQuery jsonrpc 2.0 call via .ajax() gets correct response but does not work?通过 .ajax() 调用 jQuery jsonrpc 2.0 得到正确响应但不起作用?
【发布时间】:2011-11-16 05:39:32
【问题描述】:

通过 jquery 对 Tornado Web 服务器的 jsonrpc 2.0 调用会得到一个 “200 OK”http 响应和我的网络嗅探器显示解码 响应包含

{"jsonrpc":"2.0","error":null,"result":3500,"id":"jsonrpc"}

即有效的 jsonrpc 2.0 响应。 3500也是正确的结果, RPC 是一个简单的添加函数。

但是 firebug 不显示响应和 .ajax success 回调 不触发。 .ajax() errorcomplete 回调是 触发但不让我知道问题所在。这里是 触发 ajax() 调用的 index.html。

    $(document).ready(function(){
       $.ajax({
          url: 'http://localhost:8080', 
          data: JSON.stringify ({jsonrpc:'2.0',method:'add', params:[1400,2100],id:"jsonrpc"} ),  // id is needed !!
          type:"POST",
          dataType:"json",
          success: function (result) { 
                 alert("ok");
          },
          error: function (err,status,thrown) {
                 alert ("this syntax sucks!! " + " ERROR: " + err + " STATUS: " + status + " " + thrown );
          },
          complete: function (xhr,status) { 
                 alert('Complete=>  showing status as: '+ status); 
                 data = $.parseJSON(xhr.responseText);  
                 alert (data);
          } 
       });
    });

【问题讨论】:

  • 错误和完成的状态和错误是什么?
  • 最初我使用 Firefox“文件打开”加载 index.html(如上所示)。当我浏览localhost:8080 时,我没有这样做,而是让我的 Tornado Web 服务器提供它。这彻底解决了问题。现在成功触发,我得到正确的远程过程调用结果。

标签: jquery ajax


【解决方案1】:

我发现问题在于使用 Firefox“文件打开”打开 index.html,而不是让我的网络服务器向我提供 index.html(通过浏览 http://localhost:8080

这是一个完整的工作示例,它使用一个简单的警报进行 JSON RPC 调用并显示结果。 RPC 是一个基本的加法函数。

查看实际效果:

  • 保存 index.html (2) 和 webserver.py (1)。编辑 webserver.py 到 反映 index.html 的位置

  • 启动 webserver.py (chmod a+x webserver.py.sudo ./webserver.py)

  • 启动 Firefox 并浏览到 localhost:8080。这将加载 index.html, 触发 ajax() 调用并使用警报显示结果。

(1) Web 服务器是 Tornado,使用 tornadorpc 模块并用 Python 编写。这里是:

#! /usr/bin/python2.6 

import tornado.httpserver import tornado.ioloop import tornado.web

from tornadorpc.json import JSONRPCHandler from tornadorpc import private, start_server


class MainHandler(tornado.web.RequestHandler):
    def get(self,upath):
        self.write( open('/home/travis/EXPLORE/webApps/index.html').read() )            

class Tree(object):

    def power(self, base, power, modulo=None):
        return pow(base, power, modulo)

    def _private(self):
        # Won't be callable
        return False

class Handler(JSONRPCHandler):

    print ('In Handler()...') 
    tree = Tree()

    def add(self, x, y):
        print ('add()  method called...') 
        return x+y

    def ping(self, obj):
        return obj

# Order is important here.. first matched handler in array is used !! handlers = [
            ('/RPC2',Handler),
            (r"/(.*)", MainHandler),

            ]

start_server(handlers, port=8080)

(2) index.html 使用 jquery 的 ajax() 方法对添加远程过程进行 JSONRPC 调用。确保您保存它的位置与 (1) 中的网络服务器尝试从中读取其内容的路径相匹配。

<html>
<head>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>

   <script>
      $(document).ready(function(){

         $.ajax({
            url: 'http://localhost:8080/RPC2', 

            data: JSON.stringify ({jsonrpc:'2.0',method:'add', params:[1400,2100],id:"jsonrpc"} ),  // id is needed !!

            type:"POST",

            dataType:"json",
            success:  function (data)       { alert("The result is : " + data.result);},
            error: function (err)  { alert ("Error");}

         });

      });


  </script>


</head>
<body>
  <h1> jQuery JSON RPC 2.0 demo </h1>

</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2011-01-31
    • 2013-10-03
    • 1970-01-01
    • 2018-12-18
    相关资源
    最近更新 更多