node --- 实践中理解跨域

经常可以见到.说解决跨域只要返回加上"Access-Control-Allow-Origin"头部就行…
下面从实践中一步一步的理解.
1.环境准备:

1. node.js (http://nodejs.cn/) 自行下载配置, 完毕后(cmd)输入 node --version 若显示版本号则代表成功// ps: node(中的npm)方便下载资源,node用于构建一个简单的服务器

2.新建文件结构如下:
在这里插入图片描述
3.资源准备

npm init -y    // 快速建立一个包管理文件npm install --save-dev vue vue-resource  // 下载vue 和 vue-resource 并将依赖说明添加置 pacage.json (方便版本管理)// ps命令行的根目录为 Access-Control

在这里插入图片描述
在这里插入图片描述
4.准备代码

// 1.html
<body><div id="app"></div>
</body><script src="/node_modules/vue/dist/vue.js"></script>
<script src="/node_modules/vue-resource/dist/vue-resource.js"></script>
<script>const vt = new Vue({el: "#app",created() {this.getData();},methods: {getData() {const url = 'http://127.0.0.1:3000/getData';this.$http.get(url).then((res) => {var res = res.body;console.log(res);})}},})
</script> // 说明:
// 1.欢迎留言指出
// 2.created()是vue的生命周期函数中的第2个,此时,页面中的数据和方法全部准备好,因此可以在这里调用下面的getData()方法
// 1.js
const http = require('http');
const url = require('url');const server = http.createServer();server.on('request', function(req, res) {let { pathname } = url.parse(req.url, true);if (pathname === '/getData') {let data = JSON.stringify({isSuccess: true,msg: [{name: '栗子',age: '18'}]})res.end(data);}})server.listen(3000, function() {console.log("server is running at http://127.0.0.1:3000 !");
})

5.使用node运行服务器监听请求
在这里插入图片描述
打开html页面,
在这里插入图片描述
可以很明显的看到,页面运行的端口是5500,而数据是在3000端口.因此产生了跨域。
下面使用面向百度编程提供的解决跨域的方法:

// 1.js
// 在res.end之前加上一句话
res.setHeader("Access-Control-Allow-Origin", "*");

在这里插入图片描述
重启服务器.
Ctrl + C 退出当前执行环境. 然后执行 node 1.js
在这里插入图片描述
在这里插入图片描述
可以看到请求数据成功.!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/250814.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

熟悉常用的Linux操作

cd命令&#xff1a;切换目录 &#xff08;1&#xff09; 切换到目录 /usr/local Cd /usr/local &#xff08;2&#xff09; 去到目前的上层目录 Cd .. &#xff08;3&#xff09;回到自己的主文件夹 Cd ~ ls命令&#xff1a;查看文件与目录 &#xff08;4&#xff09;查看…

2 Effect Hook

副作用&#xff1a;和外部有交互 引用外部变量调用外部函数修改dom、全局变量ajax计时器&#xff08;依赖window.setTimeout&#xff09;存储相关 纯函数&#xff1a;相同的输入一定会得到相同的输出 Effect Hook可以让你在函数组件中执行副作用操作 类组件中处理副作用 在com…

【JUC】CountDownLatch

因为在调用端的异步中&#xff0c;需要调用其他多个服务获取数据再汇总结果返回&#xff0c;所以用到了CountDownLatch CountDownLatch的概念 CountDownLatch是一个同步工具类&#xff0c;用来协调多个线程之间的同步&#xff0c;或者说起到线程之间的通信&#xff08;而不是用…

node --- Missing write access to 解决

今天在使用npm安装animate.css时报错… 大体原因是没有对node_modules没有写的权限. 百度查到是要删除对应的node_modules然后在安装… 但是我并不想这样做…想起前面我为了加快下载速度,好像使用的是cnpm… 于是我使用了nrm ls 查看当前使用的源 更换npm的源可以参考 https:…

3 useReducer及其实现

pureComponent import { useState } from "react" // useReducer, // 统一调度 function reducer(state, action) {console.log(reducer接收参数, state, action)const { type } actionswitch (type) {case add:return { num: state.num 1 }case minus:return { n…

Django 之 权限系统(组件)

参考: http://www.cnblogs.com/yuanchenqi/articles/7609586.html 转载于:https://www.cnblogs.com/bigtreei/p/8564243.html

vue踩坑- 报错npm ERR! cb() never called!

在vue项目中引入饿了么elementUI组件的步骤之中&#xff0c;出现以下的错误&#xff1a; D:\my-project-first>npm i element-ui -S Unhandled rejection RangeError: Maximum call stack size exceededill install loadIdealTreeat RegExp.test (<anonymous>)at D:\n…

maven之阿里云Maven镜像的使用

Maven中央仓库在国外&#xff0c;速度比较慢&#xff0c;所以我们采用国内的镜像&#xff0c;速度回有质的提升。 配置下setting.xml <mirrors><mirror><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/ne…

vue --- 使用animate.css实现动画

1.下载animate.css npm install --save-dev animate.css// 注意你使用的源 nrm ls(若没有改变可以忽略)2.导入animate.css <link rel"stylesheet" href"../node_modules/animate.css/animate.css"> // 注意你的当前文件和node_moudules文件夹的相对…

4 contextHook

类组件createContext、静态属性contextType 与函数组件useContext 的对比 import { Component, createContext, useContext } from react const AppContext createContext(0) class Foo extends Component {render() {return (<AppContext.Consumer>{value > (Foo: …

【leetcode 简单】 第一百一十题 分发饼干

假设你是一位很棒的家长&#xff0c;想要给你的孩子们一些小饼干。但是&#xff0c;每个孩子最多只能给一块饼干。对每个孩子 i &#xff0c;都有一个胃口值 gi &#xff0c;这是能让孩子们满足胃口的饼干的最小尺寸&#xff1b;并且每块饼干 j &#xff0c;都有一个尺寸 sj 。…

基于openstack搭建百万级并发负载均衡器的解决方案

最近&#xff0c;喜欢研究一些国外技术大咖们的文章&#xff0c;而这篇文章是基于openstack负载均衡器的解决方案&#xff0c;做的一些总结~希望能够给小伙伴带来一些灵感或者帮助。 openstack现有的负载均衡解决方案&#xff0c;无论是lbaas plugin还是octavia&#xff0c;后端…

5 useMemouseCallback

useMemo 优化渲染 现象 App每次重新执行时&#xff0c;render变化了&#xff0c;引用的render不是同一个函数 import React, { useState, } from "react"; const Foo props > {return <ul>{props.render()}</ul> } function App() {const [range…

vue --- 动画执行的周期(动画的钩子函数)

如下8个: <transitionv-on:before-enter "beforeEnter"v-on:enter "enter"v-on:after-enter "afterEnter"v-on:enter-cancelled "enterCancelled"v-on:before-leave "beforeLeave"v-on:leave "leave"v-…

二分查找c++

相信对于二分查找的原理大家已经明白&#xff0c;接下来就是代码实现了 1 #include <iostream>2 #include <cstdio>3 #include <algorithm>4 #include <cstring>5 #include <string>6 #include <cstdlib>7 8 using namespace std;9 10 in…

php获取网址

1 #测试网址: http://localhost/blog/testurl.php?id52 3 //获取域名或主机地址 4 echo $_SERVER[HTTP_HOST]."<br>"; #localhost5 6 //获取网页地址 7 echo $_SERVER[PHP_SELF]."<br>"; #/blog/testurl.php8 9 //获取网址参数 10 echo …

6 useRef、useImperativeHandle

useRef在每次执行时返回的是同一个引用&#xff08;返回的ref对象在组件的整个生命周期内保持不变&#xff09;在函数组件中可以使用useRef和createRef但useRef性能比createRef好&#xff0c;快在类组件中&#xff0c;createRef是在初始化constructor时被赋值的&#xff08;执行…

vue --- 列表(v-for渲染)的各种神仙动画效果

通过v-for生成的元素,使用transition包裹将只显示第一条数据,此时需要使用transition-group包裹. <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-wid…

linux命令目录

一、文件和目录。&#xff08;文件目录的增删改查&#xff09; lspwdcdmkdirtouchrmdirlnddrmcpmvnlcattacmorelessheadtailstat###########################################grepawksed findlocatewhichwhereiswc ############################################dfdumountumoun…

vue --- 使用component的 :is属性切换标签页

点击对应的标签,下面切换至对应的模板… // 说明 <component :is"name"></component> // 相当于把id为name的组件放到对应的位置总体代码如下: <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"…