XML-RPC Javascript 不支持的方法(“选项”)

发布于 2024-09-10 08:32:47 字数 679 浏览 4 评论 0原文

我们有一个 XML-RPC 服务器(用 python 实现),我正在尝试编写一个简单的 javascript 应用程序来向它发送调用。无论我创建什么库,我似乎总是会遇到错误:

Unsupported method ('OPTIONS')

可以公平地说,我不理解 XML-RPC 和 HTTP 的底层协议,而我应该理解。但我所知道的是,这在Python中有效:

client = xmlrpclib.ServerProxy('http://localhost:2002')
client.T.run_process()

但以下JavaScript无效:

var client = new xmlrpc_client("", "localhost", 2002, "http")
var msg = new xmlrpcmsg("T.run_process()", {})
lert(client.send(msg));

我正在使用这个 JavaScript 库。但似乎无论我使用什么库,我都会遇到相同的错误,所以我猜我们的服务器不符合 python 不介意的某些协议,对吗?

We've got an XML-RPC server (implemented in python), and I'm trying to write a simple javascript app to send calls to it. Whatever library I make I seem to always get the error:

Unsupported method ('OPTIONS')

It's fair to say I don't understand the underlying protocols for XML-RPC and HTTP as well as I should. But what I do know is that this works in python:

client = xmlrpclib.ServerProxy('http://localhost:2002')
client.T.run_process()

But the following javascript doesn't:

var client = new xmlrpc_client("", "localhost", 2002, "http")
var msg = new xmlrpcmsg("T.run_process()", {})
lert(client.send(msg));

I'm using this javascript library. But it seems I get the same error no matter what library I use, so I guess our server isn't conforming to some protocol that python doesn't mind about, is this right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰雪之触 2024-09-17 08:32:47

使用 python 中的标准 SimpleXMLRPCServer,将以下内容添加到 RequestHandler 方法似乎对我有用:

class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

    def do_OPTIONS(self):
        self.send_response(200)
        self.end_headers()

    # Add these headers to all responses
    def end_headers(self):
        self.send_header("Access-Control-Allow-Headers", 
                         "Origin, X-Requested-With, Content-Type, Accept")
        self.send_header("Access-Control-Allow-Origin", "*")
        SimpleXMLRPCRequestHandler.end_headers(self)

Using the standard SimpleXMLRPCServer from python, adding the following to the RequestHandler methods seemed to work for me:

class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

    def do_OPTIONS(self):
        self.send_response(200)
        self.end_headers()

    # Add these headers to all responses
    def end_headers(self):
        self.send_header("Access-Control-Allow-Headers", 
                         "Origin, X-Requested-With, Content-Type, Accept")
        self.send_header("Access-Control-Allow-Origin", "*")
        SimpleXMLRPCRequestHandler.end_headers(self)
似最初 2024-09-17 08:32:47

这可能是 CORS 的实际应用。

This may be CORS in action.

活雷疯 2024-09-17 08:32:47

朱利安可能是对的。请参阅此答案< /a> 了解详细信息和更多链接。

Julian is probably right. See this answer for details and some more links.

邮友 2024-09-17 08:32:47

我最近正在与类似的事情作斗争。

问题是 python 的 XMLRPC 服务器在 XML-RPC 请求中不包含 CORS 标头(也不响应 HTTP OPTIONS 请求)。

我使用 Twisted 来提供 XMLRPC 资源,并解决了向 XMLRPC 请求添加 OPTIONS 响应和标头的问题。

我的代码看起来像这样:

from twisted.web.xmlrpc import withRequest

class MyResourceEndpoint(xmlrpc.XMLRPC):
    def render_OPTIONS(self, request):    
        request.setHeader('Access-Control-Allow-Origin', '*')
        request.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')        
        request.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
        return ""

    @withRequest
    def xmlrpc_my_method(self, request, my_params):
        request.setHeader('Access-Control-Allow-Origin', '*')
        request.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')        
        request.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
        return "Whatever your method do"

root = resource.Resource()
root.putChild('', MyResourceEndpoint())
reactor.listenTCP(9090, server.Site(root))

reactor.run()

I was fighting against something similar recently.

The problem is that python's XMLRPC server does not include CORS headers (nor respond HTTP OPTIONS request) in the XML-RPC request.

I'm using Twisted to serve the XMLRPC Resource, and solved that adding the OPTIONS response and the headers to the XMLRPC request.

My code looks something like:

from twisted.web.xmlrpc import withRequest

class MyResourceEndpoint(xmlrpc.XMLRPC):
    def render_OPTIONS(self, request):    
        request.setHeader('Access-Control-Allow-Origin', '*')
        request.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')        
        request.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
        return ""

    @withRequest
    def xmlrpc_my_method(self, request, my_params):
        request.setHeader('Access-Control-Allow-Origin', '*')
        request.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')        
        request.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
        return "Whatever your method do"

root = resource.Resource()
root.putChild('', MyResourceEndpoint())
reactor.listenTCP(9090, server.Site(root))

reactor.run()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文