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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料