Origin null is not allowed by Access-Control-Allow-Origin.

后端 未结 2 1808
猫巷女王i
猫巷女王i 2021-01-06 00:19

When I am running the following code:



    
        
        

        
2条回答
  •  青春惊慌失措
    2021-01-06 00:53

    For security reasons, browsers blocks Ajax HTTP requests (XHR) from different hosts (origins).

    The d3.json("...") function makes an Ajax request to your http://localhost/... and you are probably serving the HTML from a different host.

    Are you opening the .html as file in the browser? Thats considered a different host. You have some options there:

    • Serve your HTML file from the same web server you are serving the json file
    • Convert your .json into a .js adding something like var mygeodata = {your json here} to the file and adding in the HTML while also removing the d3.json("...") part. After that you have a global variable with your data in mygeodata
    • Configure your web server to allow CORS.

    If you are studying/prototype (by the looks of it) I would go with the second approach.

提交回复
热议问题