簡體   English   中英

TypeError:$ .ajax在運行ajax請求時不是函數

[英]TypeError: $.ajax is not a function while running a ajax request

而不是curl,我使用jsonp格式從api中提取數據。 在這里,我使用wit.ai api作為樣本參考。 我使用以下格式提出了請求。

樣本卷曲得到這個

$ 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);
  }
});

然后我在終端中執行了節點app.js,產生了以下錯誤。

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?

如果您在客戶端使用Jquery,請將其包含在模板的<head>標記中:

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

如果您在服務器端使用jquery,請使用npm安裝jquery,並使用:

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

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

我個人不喜歡在客戶端使用jquery。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM