首页 > 编程语言 >在 node 中使用 jquery ajax

在 node 中使用 jquery ajax

时间:2022-08-31 19:11:42浏览次数:93  
标签:node jquery console log xhr ajax status

对于前端同学来说,ajax 请求应该不会陌生。jquery 真的ajax请求做了封装,可以通过下面的方式发送一个请求并获取相应结果:

$.ajax({
    url: "https://echo.apipost.cn/get.php",
    data: formData,
    type: "POST",
    processData: false,
    contentType: false,
    success: function (data, status, xhr) {
        console.log(data, status, xhr)
    },
    error: function (xhr, status, error) {
        console.log(xhr, status, error)
    },
    complete: function (xhr, status) {
        console.log(xhr, status)
    }
})

但是,在 node 环境中,就需要一些其他库实现接口发送,例如GOT、request 等。当然,也可以通过在node 中安装 jquery 来使用上面 $.ajax 的方法,但是会有一个很要命的问题:跨域。

何为跨域?

当一个请求url的协议、域名、端口三者之间任意一个与当前页面url不同即为跨域。也就是违反了“同源策略”:

同源策略是一个重要的安全策略,它用于限制一个origin的文档或它加载的脚本如何能与另一个源的资源进行交互。能够减少恶意文档,减少可能被攻击媒介。 如果两个URL的协议、域名、端口号都相同,就称这两个URL同源。

浏览器默认两个不同的源之间是可以互相访问资源和操作DOM的。两个不同的源之间若是想要访问资源或者操作DOM,那么会有一套基础的安全策略的制约,我们把这称为同源策略。它的存在可以保护用户隐私信息,防止身份伪造。

 

 

使用 ajax-for-node 实现 $.ajax

 ajax-for-node (https://www.npmjs.com/package/ajax-for-node)是一个在 node 环境下实现 $.ajax 的库,它的所有参数格式和 $.ajax 完全一致。

const nodeAjax = require('ajax-for-node');

var formData = new FormData();
formData.append("username", "Groucho");
formData.append("accountnum", 123456);

nodeAjax({
    url: "https://echo.apipost.cn/get.php",
    data: formData,
    type: "POST",
    processData: false,
    contentType: false,
    success: function (data, status, xhr) {
        console.log(data, status, xhr)
    },
    error: function (xhr, status, error) {
        console.log(xhr, status, error)
    },
    complete: function (xhr, status) {
        console.log(xhr, status)
    }
});

github:https://github.com/Apipost-Team/nodeajax

npm :https://www.npmjs.com/package/ajax-for-node

 

标签:node,jquery,console,log,xhr,ajax,status
From: https://www.cnblogs.com/phpwechat/p/16644248.html

相关文章

  • 修改 markdown 二级标题的编号 - 自动编号 autoNumber.js nodejs
    需求我在写https://www.vuejsdev.com/01FE/must-know-knowledge.html这个页面的时候,二级页面标题前面有编号,但是有时候我会把顺序换下,每次手动修改编号,开始数据量小还......
  • nodejs
     几点问题:1、怎么直接添加子节点2、判断目录有效性3、parseString有没有同步接口4、readFileSync有没有接口判断读文件是否成功 constfs=require("fs");cons......
  • jQuery - AJAX get() 和 post() 方法
    jQuery- AJAXget()和post()方法HTTP请求:GETvs.POST两种在客户端和服务器端进行请求-响应的常用方法是:GET和POST。GET -从指定的资源请求数据POST -向......
  • error [email protected]: The engine "node" is incompatible with this module. Exp
    [email protected]:Theengine"node"isincompatiblewiththismodule.Expectedversion"^14.18.0||>=16.0.0".Got"14.16.0"errorFoundincompatiblemod......
  • [LeetCode] 1315. Sum of Nodes with Even-Valued Grandparent 祖父节点值为偶数的节
    Giventhe root ofabinarytree,return thesumofvaluesofnodeswithan even-valuedgrandparent.Iftherearenonodeswithan even-valuedgrandparent......
  • vue3源码学习2-创建和渲染vnode
    创建vnode我们在第一节中在packages/runtime-core/src/apiCreateApp.ts文件的createAppAPI方法中,app.mount()时://通过createVNode方法创建了根组件的vnodeconstvnod......
  • 什么是 Node.js?
    什么是Node.js?Node.js是一个开源、跨平台的后端JavaScript运行环境,运行在V8引擎上,并在Web浏览器之外执行JavaScript代码,旨在构建可扩展的网络应用程序。关键......
  • NodeJS 网关 — 第 2 部分:设置我们的数据库 (MongoDB)
    NodeJS网关—第2部分:设置我们的数据库(MongoDB)Photoby鲁拜图尔·阿扎德on不飞溅NoSQL数据库使您可以轻松地开始使用基本模式开发项目或应用程序,并且由于......
  • 使用ESP8266nodeMCU 向微信推送模板数据
    使用HTTPS协议向微信公众号推送消息,(使用ESP8266的低成本实现)前几天被朋友问到这个东西的实现方式,花了一下午时间研究一下,特此记录。没有排版比较乱。      ......
  • jquery
    jquery概述:jquery是一个前端的js库,它兼容性好(处理了兼容),它的语法简洁。它是链式调用的语言。以面向对象封装的以返回一个jquery对象为核心来实现对应的链式调用。它集成......