首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CherryPy和CORS

CherryPy和CORS
EN

Stack Overflow用户
提问于 2016-04-23 03:13:09
回答 1查看 2.4K关注 0票数 1

我设置了一个nginx服务器,我想在其中设置一个接收字符串并返回结果的服务。我计划使用Python进行处理,并使用CherryPy作为接口。我已经测试了CherryPy部件,并且知道它可以正确接收。当我尝试使用网页连接到CherryPy服务时,收到CORS错误。我怎样才能让他们交流呢?

以下是Python代码:

代码语言:javascript
复制
import cherrypy
import random
import urllib

class DataView(object):
    exposed = True

    @cherrypy.tools.accept(media='application/json')

    def GET(self):
        rawData = cherrypy.request.body.read(int(cherrypy.request.headers['Content-Length']))
        b = json.loads(rawData)
        return json.dumps({'x': 4, 'c': b})

    def CORS():
        cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"

if __name__ == '__main__':
    conf = {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.CORS.on': True,
        }
    }
    cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS)
    cherrypy.config.update({'server.socket_port': 3000})
    cherrypy.quickstart(DataView(), '', conf)

这是我的网页:

代码语言:javascript
复制
<html lang="en">
<head>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
    <script type="text/javascript">

        $(document).on('click', "#submitButton", function(){
            $.ajax({

                type: 'GET',

                url: 'http://localhost:3000',

                contentType: 'text/plain',

                xhrFields: {
                    // The 'xhrFields' property sets additional fields on the XMLHttpRequest.
                    // This can be used to set the 'withCredentials' property.
                    // Set the value to 'true' if you'd like to pass cookies to the server.
                    // If this is enabled, your server must respond with the header
                    // 'Access-Control-Allow-Credentials: true'.
                    withCredentials: false
                },

                headers: {
                },

                success: function() {
                    console.log("Success");
                },

                error: function() {
                    console.log("Fail");
                }
            }); 
        });
</script>

</head>

<body>

    <div id="header">
        <h2>PDE Grammar Engine</h2>
        <form>
            Input Sentence:<br>
            <input type="text" name="query" id="query"><br>

            <input type="submit" id="submitButton" value="Submit">

        </form>
    </div>
    </div id="results">
    </div>
</body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2016-05-03 01:51:19

结果发现CherryPy服务器实际上没有侦听正确的地址。它允许来自本地主机的连接,但不允许外部连接。我必须将以下条目添加到cherrypy.config.update中

代码语言:javascript
复制
cherrypy.config.update({'server.socket_host': '0.0.0.0',
                        'server.socket_port': 3000})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36801650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档