相关疑难解决方法(0)

Cherrypy出现“不存在'Access-Control-Allow-Origin'标头”错误

我在HTML页面中有以下javascript

<script>
    function getContent(page)
    {
        var xmlhttp;
        if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
        else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
             {
                var json = xmlhttp.responseText;
                obj = JSON.parse(json);
                document.getElementById("content").innerHTML=obj.content;
                document.getElementById("title").innerHTML=obj.title;
             }
        }
    xmlhttp.open("GET","http://differentserver.com:8080?page="+page,true);
    xmlhttp.send();
}
</script>
Run Code Online (Sandbox Code Playgroud)

和一个使用cherrypy的python脚本,该脚本提供JSON,其代码为:

import cherrypy
import json

class ContentGeneratorService(object):
exposed = True
@cherrypy.tools.accept(media='text/plain')
def GET(self, page='home'):
    file_title = open(page + '.title', 'r')
    file_content = open(page + '.content', 'r')
    return json.dumps({"title": file_title.read().replace('\n', …
Run Code Online (Sandbox Code Playgroud)

javascript python json cherrypy

4
推荐指数
1
解决办法
3230
查看次数

标签 统计

cherrypy ×1

javascript ×1

json ×1

python ×1