TypeError:_jquery2.default.ajax不是函数

时间:2017-02-02 01:16:47

标签: javascript express typeerror

TypeError: _jquery2.default.ajax is not a function
at Object.login (/Users/Vinit/Documents/Bookr/src/util/uber/auth_api.js:19:12)
at /Users/Vinit/Documents/Bookr/server.js:22:21
at Layer.handle [as handle_request] (/Users/Vinit/Documents/Bookr/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/Vinit/Documents/Bookr/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/Vinit/Documents/Bookr/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/Vinit/Documents/Bookr/node_modules/express/lib/router/layer.js:95:5)
at /Users/Vinit/Documents/Bookr/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Users/Vinit/Documents/Bookr/node_modules/express/lib/router/index.js:330:12)
at next (/Users/Vinit/Documents/Bookr/node_modules/express/lib/router/index.js:271:10)
at /Users/Vinit/Documents/Bookr/server.js:14:3

这是我收到的错误。我尝试使用uber的api并且我收到了授权码但需要发帖子请求将其换成访问令牌。这是我的服务器代码:

const app = express();

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', "http://localhost:3000");
  res.header('Access-Control-Allow-Methods', 'DELETE');
  res.header('Access-Control-Allow-Credentials', true);
  next();
});

app.use('/app', express.static(path.join(__dirname, './app')));

app.get('/auth', (req, res) => {
  res.json(UberAuth.login(req.query.code));
});

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, './index.html'));
});

app.listen(PORT, () => {
  console.log(`server started on port ${PORT}`);
});

问题出在app.get(' / auth' ....)部分。出于某种原因<当我尝试拨打“登录”时我定义的功能:

export const login = (authCode) => {
  console.log(authCode);
  return $.ajax({
    url: "https://login.uber.com/v2/token",
    method: "POST",
    dataType: 'jsonp',
    data: {
      client_id: UBER_CLIENT_ID,
      client_secret: UBER_CLIENT_SECRET,
      grant_type: 'authorization_code',
      redirect_uri: REDIRECT_URI,
      code: authCode,
      scope: 'profile'
    }
  });
};

我真的不明白为什么我会收到此错误,因为我的程序中的其他地方,jquery工作正常。我已经尝试了很多东西,无法解决这个问题。我是新来的,我很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

您似乎没有正确导入jQuery以在登录功能中使用。此时确保$变量具有ajax函数。