CherryPy and CORS
Question: I have an nginx server set up where I would like to set up a service that
receives a string and returns a result. I plan to use Python to do the
processing, with CherryPy as an interface. I've tested the CherryPy part, and
know it receives properly. When I try to connect to the CherryPy service with
a web page, I get CORS errors. How can I get them to communicate?

Here's the Python Code:

    
    
    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)
    

Here's my web page:

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


Answer: Turned out that the CherryPy server was not actually listening to the correct
address. It was allowing connections from localhost, but not external
connections. I had to add the following entry to the cherrypy.config.update

    
    
    cherrypy.config.update({'server.socket_host': '0.0.0.0',
                            'server.socket_port': 3000})
    

