类型错误:$.ajax 在运行 ajax 请求时不是函数 [英] TypeError: $.ajax is not a function while running a ajax request

查看:20
本文介绍了类型错误:$.ajax 在运行 ajax 请求时不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jsonp 格式从 api 中提取数据,而不是 curl.这里我使用 wit.ai api 作为示例参考.我已使用以下格式提出请求.

Instead of curl, I'm using jsonp format to extract data from the api. Here I'm using wit.ai api for sample reference. I have made a request using the following format.

样本卷曲得到这个

$ curl \
      -H 'Authorization: Bearer $TOKEN' \
      'https://api.wit.ai/message?v=20160526&q=hello'

现在我的代码

var $ = require('jquery');

$.ajax({
  url: 'https://api.wit.ai/message',
  data: {
    'q': 'set an alarm in 10min',
    'access_token' : 'MY_WIT_TOKEN'
  },
  dataType: 'jsonp',
  method: 'GET',
  success: function(response) {
      console.log("success!", response);
  }
});

然后我在终端中执行了 node app.js,它给出了以下错误.

And then I executed node app.js in the terminal which gave the following error.

c:\app.js:3
$.ajax({
  ^

TypeError: $.ajax is not a function
    at Object.<anonymous> (c:\app.js:3:3)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

为什么我收到错误消息?有没有其他方法可以使用终端运行ajax?

Why I'm getting the error? Is there any other way to run ajax using the terminal?

推荐答案

如果您在客户端使用 Jquery,请将其包含在模板的 标记中:

If you are using Jquery on the client side, include this in the <head> tag of your template:

<script src="jquery-3.1.1.min.js"></script>

如果您在服务器端使用 jquery,请使用 npm 安装 jquery,并使用:

If you are using jquery on the server side, install jquery using npm, and use this:

require("jsdom").env("", function(err, window) {
    if (err) {
        console.error(err);
        return;
    }

    var $ = require("jquery")(window);
});

我个人不喜欢在客户端使用 jquery.

Personally I will not prefer using jquery on the client side.

这篇关于类型错误:$.ajax 在运行 ajax 请求时不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆