[英]Getting cors error accessing Azure node.js server
我有一个简单的 node.js 应用程序在 azure 上运行,具有正常的 CRUD 获取/设置/放置/等 api。 在 Azure 上也可以从另一个客户端应用程序访问。 如果我在本地计算机上运行两者,它们运行良好。 我将它们发布到 Azure AppServices,客户端“get”(获取所有记录)工作,但删除、更新和添加都失败了
polyfills-es2015.d3e61f0dd2b58e518eca.js:1 Access to XMLHttpRequest at 'hhttps://kurttaskelementmongo.azurewebsites.net/elements/6' from origin ' https://kurtmongoangular.azurewebsites.net ' has been blocked by CORS policy: Cross origin请求仅支持协议方案:http、data、chrome、chrome-extension、https。
I have gone into the Azure portal and get the Cors setting for the node.js server to * as all the web articles say, and I have had other apps work fine. 我希望还有其他一些我遗漏的考虑因素导致此失败。
这是客户端代码(使用 Angular 的 http 模块)
这是进行调用的 angular 代码:
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { Task } from './Task';
export interface Tasks { // I have no idea what this does!!!
name: string;
}
@Injectable({ providedIn: 'root' }) // this makes this service injectable, Angular's dependecy injection model
export class TaskService {
constructor(private http: HttpClient) {}
getAllTasks(): Observable<Task[]> {
return this.http.get<Task[]>(' https://kurttaskelementmongo.azurewebsites.net/tasks/');
}
getTask(taskName: string): Observable<Task> {
return this.http.get<Task>('https://kurttaskelementmongo.azurewebsites.net/tasks/' + taskName);
}
insertTask(task: Task): Observable<Task> {
return this.http.post<Task>('https://kurttaskelementmongo.azurewebsites.net/tasks/', task);
}
updateTask(task: Task): Observable<void> {
return this.http.put<void>('https://kurttaskelementmongo.azurewebsites.net/tasks/' + task._id, task);
}
deleteTask(task: Task) {
return this.http.delete('https://kurttaskelementmongo.azurewebsites.net/tasks/' + task._id);
}
}
//这是 node.js app.js
const express = require("express");
const bodyParser = require("body-parser");
const taskController = require("./controllers/TaskController");
const elementController = require("./controllers/ElementContoller");
// this brings in and sets up the monog db instance connection
require("./config/db");
const app = express();
const port = process.env.PORT || 80; // setting the port number for this server
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// API ENDPOINTS
// not using the Express Router code, instead just listing them
app
.route("/tasks")
.get(taskController.listAllTasks)
.post(taskController.createNewTask);
app
.route("/tasks/:taskid")
.get(taskController.readTask)
.put(taskController.updateTask)
.delete(taskController.deleteTask);
// add new routes for my element table demo of Elements
app
.route("/elements")
.get(elementController.listAllElements)
.post(elementController.createNewElement);
app
.route("/elements/:_id")
.get(elementController.readOneElement)
.put(elementController.updateElement)
.delete(elementController.deleteElement);
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
sideshowbaker 是正确的!
额外的 h 意味着它不是 Cors 需要的 http、https 等。 看不出这是如何破坏 /tasks 接口和 /elements 接口的,但确实如此。
所以对我的“还能是什么”问题的更一般的答案是:
如果您尝试使用“支持的协议方案:http, data, chrome, chrome-extension, https”以外的任何内容访问服务器,它将给您一个 cors 错误。 这包括错字!
非常感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.