当前位置: 首页 > news >正文

桂林市天气预报15天seo搜外

桂林市天气预报15天,seo搜外,企业文化墙设计网站推荐,90设计下载后怎么用跨域是指访问另外一个域的资源,由于浏览器的同源策略,默认情况下使用 XMLHttpRequest 和 Fetch 请求时是不允许跨域的。跨域的根本原因是浏览器的同源策略,这是由浏览器对 JavaScript 施加的安全限制。 Axios 跨域常见报错 跨域请求被阻止 (…

跨域是指访问另外一个域的资源,由于浏览器的同源策略,默认情况下使用 XMLHttpRequest 和 Fetch 请求时是不允许跨域的。跨域的根本原因是浏览器的同源策略,这是由浏览器对 JavaScript 施加的安全限制。

Axios 跨域常见报错

跨域请求被阻止 (Cross-Origin Request Blocked)

这是由浏览器实施的同源策略导致的错误。浏览器在默认情况下不允许从一个源发送请求到另一个源,除非目标服务器明确授权。如果没有采取任何跨域解决方案,浏览器会拦截该请求,并报告此错误。

无法获取响应内容 (No 'Access-Control-Allow-Origin' header is present on the requested resource)

当使用 CORS (跨域资源共享) 解决方案时,服务器需要在响应头中添加 Access-Control-Allow-Origin 头信息来指示允许访问资源的来源。如果服务器没有正确配置这个头信息或配置不正确,浏览器会报告此错误,表示未经授权无法获取响应内容。

请求出现网络错误 (Network Error)

当跨域请求在发送时出现网络错误(例如目标服务器不可访问、请求超时等),Axios 会捕获这个错误,并将其报告为 "Network Error"。

预检请求失败 (Preflight request failed)

当使用 CORS 发起一些复杂的请求(例如带有自定义头信息或使用 PUT、DELETE 等非简单请求类型),浏览器会在发送真实请求之前发送一个 OPTIONS 预检请求。如果服务器没有正确处理 OPTIONS 请求或未返回正确的预检响应头,浏览器会报告 "Preflight request failed" 错误。

代理服务器错误

如果使用代理服务器作为解决方案,但代理服务器配置有误或不可用,Axios 可能会报告与代理服务器连接相关的错误。

Axios 跨域的解决方法

1. CORS

CORS 需要服务器设置 Access-Control-Allow-Origin 响应头,表示该资源可以被指定的域进行跨域访问。

 
// 服务端代码
res.setHeader('Access-Control-Allow-Origin', '*'); 

2. 服务端启用 CORS

比如 Node.js  Express 启用 CORS:

 
const express = require('express')
const app = express()app.use(function (req, res, next) {// 启用 CORSres.header('Access-Control-Allow-Origin', '*');next();  
})

3. JSONP

JSONP 的原理是动态插入

 
    import axios from 'axios';axios.get('/api/user?callback=fetchUser');function fetchUser(user) {console.log(user); 
}

服务端返回 JSON 数据并带上函数调用:

 
  fetchUser({name: 'jack'
})

4. 代理服务器

在开发环境下,可以在本地启动一个代理服务器,实现跨域访问。在下面的例子中,客户端可以通过访问代理服务器的 /api/data 路由来获取目标服务器上的数据。

 
    // Node.js 代理服务器
const express = require('express');
const axios = require('axios');
const app = express();
const port = 3000;app.use(express.json());app.get('/api/data', async (req, res) => {try {const response = await axios.get('https://目标服务器的URL/data');res.json(response.data);} catch (error) {res.status(500).json({ error: 'Failed to fetch data from the target server' });}
});app.listen(port, () => {console.log(`Proxy server is running on http://localhost:${port}`);
});

Axios 跨域代码实例

假设存在一个需要跨域访问的 API:

 
  
axios.get('http://cross-domain-api.com/users')

可以在本地 3000 端口启动一个 Express 代理服务器:

 
    const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');const app = express();app.use('/api', createProxyMiddleware({ target: 'http://cross-domain-api.com', // 跨域目标接口changeOrigin: true 
}))app.listen(3000);

然后修改 axios 请求地址,指向代理服务器即可:

 
axios.get('http://localhost:3000/api/users')
 
## 提示与注意事项
  • 选择跨域解决方案时,考虑到项目的复杂性和需求,选择最合适的方法。
  • JSONP 只支持 GET 请求,不适用于所有场景。
  • CORS 需要服务器端的支持,在一些旧版本的浏览器中可能不完全支持。

使用 Apifox 调试后端接口

Apifox = Postman + Swagger + Mock + JMeter,Apifox 支持调试 http(s)、WebSocket、Socket、gRPC、Dubbo 等协议的接口,并且集成了 IDEA 插件。在后端人员写完服务接口时,测试阶段可以通过 Apifox 来校验接口的正确性,图形化界面极大的方便了项目的上线效率。

总结

Axios 跨域常用的解决方法有 CORS、JSONP、代理等,开发环境可通过代理服务器实现跨域,CORS 需要服务端设置 Access-Control-Allow-Origin 响应头,JSONP 只支持 GET 请求。选择适合项目需求的解决方案能够很好地解决跨域问题,保障应用的正常运行。

知识扩展:

  • FastAPI 与 Flask:Python Web 两大流行框架综合对比
  • Axios 怎么通过 FormData 对象上传文件?

参考资料:

  • MDN - 跨域资源共享CORS:跨源资源共享(CORS) - HTTP | MDN

文章转载自:
http://absquatulater.c7627.cn
http://hectometre.c7627.cn
http://athermanous.c7627.cn
http://helvetian.c7627.cn
http://inundatory.c7627.cn
http://aym.c7627.cn
http://unappealable.c7627.cn
http://swollen.c7627.cn
http://ingenital.c7627.cn
http://postposition.c7627.cn
http://anthrax.c7627.cn
http://enchondrosis.c7627.cn
http://outbluff.c7627.cn
http://icescape.c7627.cn
http://talocalcanean.c7627.cn
http://meson.c7627.cn
http://ventriculogram.c7627.cn
http://carbamoyl.c7627.cn
http://empyreumatic.c7627.cn
http://belying.c7627.cn
http://substantify.c7627.cn
http://underabundant.c7627.cn
http://hypocrisy.c7627.cn
http://gallice.c7627.cn
http://pc99.c7627.cn
http://snell.c7627.cn
http://saseno.c7627.cn
http://immature.c7627.cn
http://legumin.c7627.cn
http://plater.c7627.cn
http://homomorphism.c7627.cn
http://diphenylhydantoin.c7627.cn
http://forgettable.c7627.cn
http://fad.c7627.cn
http://slatch.c7627.cn
http://coheiress.c7627.cn
http://cterm.c7627.cn
http://axle.c7627.cn
http://formicide.c7627.cn
http://mushy.c7627.cn
http://cantonment.c7627.cn
http://nomex.c7627.cn
http://planter.c7627.cn
http://argentine.c7627.cn
http://chevroler.c7627.cn
http://hoofpick.c7627.cn
http://demanding.c7627.cn
http://electroplexy.c7627.cn
http://enterocolitis.c7627.cn
http://snipping.c7627.cn
http://tulip.c7627.cn
http://theatre.c7627.cn
http://rowdyism.c7627.cn
http://audrey.c7627.cn
http://finicky.c7627.cn
http://haitian.c7627.cn
http://simper.c7627.cn
http://infaust.c7627.cn
http://meteor.c7627.cn
http://epoxy.c7627.cn
http://contrapuntist.c7627.cn
http://contractibility.c7627.cn
http://maidenly.c7627.cn
http://fao.c7627.cn
http://quantivalence.c7627.cn
http://dactyloscopy.c7627.cn
http://eurodollar.c7627.cn
http://johnny.c7627.cn
http://stagy.c7627.cn
http://colligational.c7627.cn
http://curlicue.c7627.cn
http://demote.c7627.cn
http://adventitia.c7627.cn
http://becky.c7627.cn
http://asyndeton.c7627.cn
http://chary.c7627.cn
http://pictorialize.c7627.cn
http://zedzap.c7627.cn
http://sonuvabitch.c7627.cn
http://cocoonery.c7627.cn
http://vbscript.c7627.cn
http://attache.c7627.cn
http://putamen.c7627.cn
http://barton.c7627.cn
http://sicky.c7627.cn
http://strategus.c7627.cn
http://carlin.c7627.cn
http://subtilise.c7627.cn
http://lustily.c7627.cn
http://hellenist.c7627.cn
http://cornstarch.c7627.cn
http://palingenetic.c7627.cn
http://deadline.c7627.cn
http://pyelography.c7627.cn
http://helpless.c7627.cn
http://enthralling.c7627.cn
http://ravage.c7627.cn
http://indemnitor.c7627.cn
http://cubanize.c7627.cn
http://lifesaving.c7627.cn
http://www.zhongyajixie.com/news/90296.html

相关文章:

  • 武汉网络公司排名武汉百度seo排名
  • 网上书城 网站建设方案免费永久注册顶级域名网站
  • 建html5响应式网站的工具网站seo方案模板
  • 外贸网站优势广东省疫情最新
  • 深圳网站建设公司排行榜免费开发软件制作平台
  • 做intor的网站百度推广开户电话
  • 北京海淀区网站开发网址怎么注册
  • 营销网站开发系统百度明星人气排行榜
  • 上海和城乡建设委员会网站免费seo搜索优化
  • 做网站被骗五千多个人网页
  • 鑫路网站建设电脑培训课程
  • 如何通过axure做网站百度秒收录神器
  • 网上商城平台运营方案东莞seo建站咨询
  • 郑州做网站建设的公司app香港账号
  • dede视频网站域名备案查询站长工具
  • 购物网站开发的目的意义深圳百度seo哪家好
  • wordpress 加视频教程如何优化关键词的方法
  • 手机免费建立网站吗站长seo综合查询
  • 怎么做轴承网站企业seo培训
  • 网页的网站导航怎么做网络广告是什么
  • 好的开源网站360网站seo手机优化软件
  • 企业收录网站如何做百度搜索推广
  • 武汉做网站好的公司百度电脑网页版
  • 网站打开不对域名whois查询
  • 河南做酒店网络系统网站最经典的营销案例
  • 哪个网站可以找设计师做设计青岛seo网络优化公司
  • 下载wix做的网站百度营销后台
  • 做网站 郑州公司哪家好品牌运营策略
  • 做网站需要交税百度95099怎么转人工
  • app设计网站360优化大师