Acess to XMLHttpRequest at http://localhost:xx from origin http://localhost has been blocked

时间:2025-01-20 22:44:19

一、本机开启nodejsserver,在通过本地网页访问的时候,出现如上错误

1 解决方案一

// Set the Access-Control-Allow-Origin header
((req, res, next) => {
  ("Access-Control-Allow-Headers", "*");
  ('Access-Control-Allow-Origin', '*');
  next();
});

2 解决方案二

var cors = require('cors')

(cors()) // Use this after the variable declaration

二 get value from json in javascript

let person = {"name", "john"}

name=

if the property name constains special characters or spaces , needuse bracket notation

person={"first name": "jonh"}

firstname=person['first name']

The `${}` syntax can also be used to perform calculations or call functions inside a string template. For example:

const price = 10;

const quantity = 5;

(`The total cost is ${price * quantity}.`);