使用油猴脚本,安装本地,想把某个数据接口转发到 localhost 的服务上,但是转发时候会有跨域问题,有解决方法吗?
// ==UserScript== // @name iwencai api 拦截 // @namespace http://tampermonkey.net/ // @description 使用 ajaxhook , 拦截全局网络请求. // @version 0.1 // @author Plzbefat // @match #target match# // @require https://cdn.jsdelivr.net/npm/[email protected]/dist/ajaxhook.min.js // @require https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js // @grant none // @downloadURL https://update.greasyfork.org/scripts/432519/js%E6%8B%A6%E6%88%AA%E5%85%A8%E5%B1%80%E7%BD%91%E7%BB%9C%E8%AF%B7%E6%B1%82.user.js // @updateURL https://update.greasyfork.org/scripts/432519/js%E6%8B%A6%E6%88%AA%E5%85%A8%E5%B1%80%E7%BD%91%E7%BB%9C%E8%AF%B7%E6%B1%82.meta.js // ==/UserScript== (function() { 'use strict'; ah.proxy({ //请求发起前进入 onRequest: (config, handler) => { //console.log("发生请求,请求地址:"+config.url) handler.next(config); }, //请求发生错误时进入,比如超时;注意,不包括 http 状态码错误,如 404 仍然会认为请求成功 onError: (err, handler) => { //console.log("发生错误,错误信息:"+err.type) handler.next(err) }, //请求成功后进入 onResponse: (response, handler) => { if(response.config.url == '#target url#'){ console.log("请求成功,反馈信息:"+response.response) handler.next(response) axios.post('http://localhost:8080/test',response.response); } } }) })(); 