Didn"t forge 2016-07-19 13:12 采纳率: 25%
浏览 105

$ .ajax不是函数吗?

I have recently started to learn Javascript and I am working on a small project. I am having an error stating "$.ajax is not a function". endpoint, token, and body are defined variables.

var $ = require('jQuery');
$.ajax({
    type: 'POST',
    url: endpoint + token,
    contentType: 'application/json',
    data: JSON.stringify(body),
    success: function(data, status) {
        console.log(data);
        console.log(status);
    },
    error: function(jqXHR, status, errorThrown) {
        console.log(errorThrown);
        console.log(status);
        console.log(jqXHR);
    }
});

Anyone see a problem with what I have?

  • 写回答

1条回答 默认 最新

  • 三生石@ 2016-07-19 13:24
    关注

    2 Possible Reasons :

    1. Check if jQuery has been added in your index.html (or whatever name you main html has) file.
    2. If above does not relate to your problem, then you need to put a check i.e. is jQuery is available to you, something like below code :

      if(window.jQuery)
          { 
            ...do your stuff here
           }
            else{
                  ... handle it according to your need here
                 }
      
    3. Also, don't forget to check if your document object is ready, if you are going to fetch a value from html inputs.

       $( document ).ready(function() {
          console.log( "ready!" );
       });
      
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号