Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery jsonrpc 2.0调用via.ajax()得到正确响应,但不起作用?_Jquery_Ajax - Fatal编程技术网

jQuery jsonrpc 2.0调用via.ajax()得到正确响应,但不起作用?

jQuery jsonrpc 2.0调用via.ajax()得到正确响应,但不起作用?,jquery,ajax,Jquery,Ajax,通过jquery调用Tornado web服务器的JSONRPC2.0会得到一个 “200OK”http响应,我的网络嗅探器显示解码的 响应为包含 {“jsonrpc”:“2.0”,“错误”:null,“结果”:3500,“id”:“jsonrpc”} i、 创建一个有效的jsonrpc 2.0响应。3500也是正确的结果, RPC是一个简单的add函数 但是firebug不显示响应和.ajax成功回调 不会触发。.ajax()错误和完成回调是无效的 但请不要告诉我有关问题的任何线索。这是你的电

通过jquery调用Tornado web服务器的JSONRPC2.0会得到一个 “200OK”http响应,我的网络嗅探器显示解码的 响应为包含

{“jsonrpc”:“2.0”,“错误”:null,“结果”:3500,“id”:“jsonrpc”}

i、 创建一个有效的jsonrpc 2.0响应。3500也是正确的结果, RPC是一个简单的add函数

但是firebug不显示响应和.ajax成功回调 不会触发。.ajax()错误完成回调是无效的 但请不要告诉我有关问题的任何线索。这是你的电话号码 触发ajax()调用的index.html


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

下面是一个完整的工作示例,它使用一个简单的警报进行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) html使用jquery的ajax()方法对addremote过程进行JSONRPC调用。确保保存位置与(1)中的Web服务器尝试读取其内容的路径相匹配

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

$(文档).ready(函数(){
$.ajax({
网址:'http://localhost:8080/RPC2', 
数据:JSON.stringify({jsonrpc:'2.0',方法:'add',参数:[14002100],id:'jsonrpc'}),//需要id!!
类型:“POST”,
数据类型:“json”,
成功:函数(数据){alert(“结果是:“+data.result”);},
错误:函数(err){alert(“error”);}
});
});
jQuery JSON RPC 2.0演示

error-in-error和complete的状态是什么?最初我是使用Firefox“文件打开”加载index.html(如上所示)。我没有这样做,而是让我的Tornado web服务器在我浏览时提供它。这彻底解决了问题。成功现在触发,我得到正确的远程过程调用结果。
<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>