gpt4 book ai didi

javascript - 启用 CORS 的正确位置在哪里?

转载 作者:太空宇宙 更新时间:2023-11-04 09:01:54 26 4
gpt4 key购买 nike

我正在使用 Spyne (示例 "hello world" code )制作一个生成一些 json 数据的网络服务,然后我试图在客户端浏览器的 javascript 代码中使用这些数据。

当我转到地址 http://localhost:8000/say_hello?name=Dave×=3 时,我得到以下输出:

["你好,戴夫","你好,戴夫","你好,戴夫"]

这就是为什么我认为与服务器无关(它按预期工作)。

我使用以下代码从此网络服务获取数据:

<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script src="jquery-1.11.1.min.js" ></script>
<script>
var request_url = 'http://localhost:8000/say_hello?name=Dave&times=3';
$.ajax( {
type:'Get',
url:request_url,
dataType: "jsonp",
crossDomain : true,
success:function(data) {
alert(data);
},
error: function()
{
alert("fail");
},

});
</script>
</body>
</html>

然后我得到“失败”弹出窗口。

当我在网上搜索时,我能找到的是在服务器端进行的如下设置:

Add following header in the server: 

Header set Access-Control-Allow-Origin *
  1. 如果必须在服务器端更改任何 header 设置,我该怎么做?
  2. 如果不需要更改任何服务器端设置,正确的客户端代码应该是什么?

编辑

这是 python 和 javascript 代码的最新版本:

HTML:

<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script src="jquery-1.11.1.min.js" ></script>
<script>
var request_url = 'http://localhost:8000/say_hello?name=Dave&times=3';
var jdata = 'none'
$.ajax( {
type:'Get',
url:request_url,
dataType: "html",
crossDomain : true,
success:function(data) {
alert(data);
},
error: function()
{
alert("fail");
},

});
</script>
</body>
</html>

python :

#!/usr/bin/env python
# encoding: utf8

'''
This is a simple HelloWorld example to show the basics of writing a Http api
using Spyne. Here's a sample:

$ curl http://localhost:8000/say_hello?name=Dave\&times=3
["Hello, Dave", "Hello, Dave", "Hello, Dave"]
'''


import logging

from spyne.application import Application
from spyne.decorator import srpc
from spyne.protocol.json import JsonDocument
from spyne.protocol.http import HttpRpc
from spyne.service import ServiceBase
from spyne.model.complex import Iterable
from spyne.model.primitive import UnsignedInteger
from spyne.model.primitive import String
from spyne.server.wsgi import WsgiApplication

class CorsService(ServiceBase):
origin = '*'

def _on_method_return_object(ctx):
ctx.transport.resp_headers['Access-Control-Allow-Origin'] = \
ctx.descriptor.service_class.origin

CorsService.event_manager.add_listener('method_return_object',
_on_method_return_object)


class HelloWorldService(CorsService):

@srpc(String, UnsignedInteger, _returns=Iterable(String))
def say_hello(name, times):

for i in range(times):
#yield '%s("Hello, %s")' % (callback, name)
yield {"name": 'Hello (%d): %s' % (i, name), "address": "%d + %d" % (i, i)}


if __name__=='__main__':
from wsgiref.simple_server import make_server
logging.basicConfig(level=logging.DEBUG)

application = Application([HelloWorldService], 'spyne.examples.hello.http',

in_protocol=HttpRpc(validator='soft'),

out_protocol=JsonDocument(ignore_wrappers=True),
)

wsgi_application = WsgiApplication(application)

server = make_server('0.0.0.0', 8000, wsgi_application)

logging.info("listening to http://127.0.0.1:8000")
logging.info("wsdl is at: http://localhost:8000/?wsdl")

server.serve_forever()

最佳答案

您需要将其添加为服务实现的第一行:

ctx.transport.resp_headers['Access-Control-Allow-Origin'] = '*'

但是,这很快就会变得非常烦人,所以这里有一种正确实现它的方法:

class CorsService(ServiceBase):
origin = '*'

def _on_method_return_object(ctx):
ctx.transport.resp_headers['Access-Control-Allow-Origin'] = \
ctx.descriptor.service_class.origin

CorsService.event_manager.add_listener('method_return_object',
_on_method_return_object)

因此,您现在可以使用 CorsService 作为服务的父类,而不是使用 ServiceBase 来自动获取 CORS header 。

另请注意,仅将 header 值设置为托管 Spyne 服务的域比使用通配符更安全。

关于javascript - 启用 CORS 的正确位置在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24516280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com