search

Home  >  Q&A  >  body text

javascript - js then error report solution


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!

阿神阿神2786 days ago864

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-05-24 11:35:28

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-05-24 11:35:28

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

    reply
    0
  • 阿神

    阿神2017-05-24 11:35:28

    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).

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-24 11:35:28

    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.

    reply
    0
  • Cancelreply