javascript - js then error report solution
阿神
阿神 2017-05-24 11:33:28
0
4
869


I used then for this ajax, but an error was reported. After studying for a long time, I don’t know where the problem lies. Please solve it!


It shows then is undefined. I don’t understand why this error occurs!

阿神
阿神

闭关修行中......

reply all(4)
Ty80

What is your jQuery version? Here in SF it can be written like this. No problem.


Your sticker is Zepto

I tried it and it didn’t work.

洪涛

jQuery’s ajax API cannot be directly then. You can wrap it as a Promise and do it like this:

// 将异步操作封装在一个返回 Promise 的函数中
function getData () {
  return new Promise((resolve, reject) => {
    $.ajax({
      url: 'xxx',
      success: data => resolve(data)
      error: err => reject(err)
    })
  })
}

// 在返回的 Promise 对象 then 方法中获取数据
// 解耦异步逻辑
getData().then(data => {
  console.log(data)
})
阿神

Thank you for the invitation.

What I saw when I saw the error message was zepto.min.js. Generally speaking, it should be caused by the callbacks and promises modules not being loaded (unlike jQuery, which is a single file, zepto is scattered in many modules. If you use the builder website to select Once the zepto.min generated by the module is fine, there will be no problem, and most of the zeptos that provide direct download or CDN are the version with only 5 modules by default).

習慣沉默

Thank you for the invitation. The brother upstairs has already said it very well. For http requests, axios.js is a better library that directly supports .then().
Let’s talk about the topic of extension. The current trend is to use async / await as much as possible, and promise is gradually withdrawing from the stage.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template