问题
I'm trying to learn ajax and I don't know back end language yet so I'm using firebase from google and I am running my code through localhost:8000 using python 2.7. I'm trying to GET/POST to this server:
https://ajax-practice-1f1b9.firebaseio.com/
If you can't access it, my JSON looks like this:
[
{
"id": 1,
"name" : "Will",
"drink": "American with creme"
},
{
"id": 2,
"name": "Donat",
"drink": "Vanilla Macchiato"
}
]
here's my HTML and javascript
$(function() {
$.ajax({
type: 'GET',
url: 'https://ajax-practice-1f1b9.firebaseio.com',
success: function(data) {
console.log(data);
}
});
});
<!doctype HTML>
<html>
<head>
<meta charset="utf-8">
<title>AJAX jQuery course</title>
<!-- latest bootstrap css -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<h1>JQuery Ajax Tutorial</h1>
<h2>Coffee Order</h2>
<ulid="orders"></ul>
<p>name: <input type="text" id="name"></p>
<p>drink: <input type="text" id="drink"></p>
<button id="add-order">Add</button>
<!-- <script src="https://www.gstatic.com/firebasejs/5.3.1/firebase.js"></script> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="index.js"></script>
</body>
</html>
I get this error on the console:
Failed to load https://console.firebase.google.com/project/ajax-practice-1f1b9/database/data/: Redirect from 'https://console.firebase.google.com/project/ajax-practice-1f1b9/database/data/' to 'https://accounts.google.com/ServiceLogin?passive=1209600&osid=1&continue=https://console.firebase.google.com/project/ajax-practice-1f1b9/database/data/&followup=https://console.firebase.google.com/project/ajax-practice-1f1b9/database/data/' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
I did something similar using axios and ReactJs a while back and it worked without problems.
回答1:
The URL https://ajax-practice-1f1b9.firebaseio.com will try to load the database in the Firebase Database console. You are probably trying to get the data through Firebase's REST API, which requires that the URL ends in .json.
So to get the JSON for that location, use https://ajax-practice-1f1b9.firebaseio.com/.json.
来源:https://stackoverflow.com/questions/51795313/failed-to-load-firebase-redirected-error