【问题标题】:how to do ajax callback in javascript如何在javascript中做ajax回调
【发布时间】:2019-10-11 14:03:54
【问题描述】:

我的场景是,我在 div 中加载了静态数据,加载后我需要进行 ajax 调用并用新数据覆盖静态数据,如何在 javascript 中执行以下操作

由于我是 lit-element 的新手,我不知道如何使用回调函数有点困惑。 加载 div 后,我需要进行 ajax 调用并用新数据覆盖静态数据。我被卡住了,请帮助或任何替代方法

import { LitElement, html, css } from 'https://unpkg.com/@polymer/lit-element/lit-element.js?module';
export class Example extends LitElement {
  static get properties() {
    return {
      staticobj: {type: Object}
   }
 }

constructor() {
    super();
   this.static=[{
     id: "value1",
     country: "TH",
     fee: 100   
   },{
    id:"value2",
    country: "SG",
    fee: 200
  }]
}

handleCall(id){
 $.ajax({
   url: "/en",
   method: 'get',
   global: false,
   async: false,
   data: {
     value: id
   },
   success: function (data) {
     callback(data, passData)
   }
 })
this.static=data; //override the static data
}

render(){
   this.static.map((e)=>{  
    return html`
   <div id="list">// call the ajax function once div loaded
     <p>${e.id}</p>
     <h6>${e.country}</h6>
     <h5>${e.fee}</h5>

  </div>
   `
   })
 }
}

【问题讨论】:

    标签: javascript ajax express lit-element


    【解决方案1】:

    您应该进入 ES6 和 LitElement 思维模式。忘记 JQuery 并使用 fetch(最好使用 ES8 async/await)。在 LitElement 的 firstUpdated() 中获取您的数据。然后替换您的数组本身(如果您替换数组元素本身,则不会看到更改,因为不会呈现更改):

    import { html, css, LitElement } from 'lit-element';
    
    export default class FetchLit extends LitElement {
      static get styles() {
        return css`
          :host {
            display: block;
            padding: 5px;
          }
        `;
      }
    
      static get properties() {
        return {
          users: { type: Array },
        };
      }
    
      constructor() {
        super();
        this.users = [
          {
            firstName: 'Jane',
            lastName: 'Doe',
            email: 'jane.doe@somewhere.com',
          },
        ];
      }
    
      // Don't use connectedCallback() since it can't be async
      async firstUpdated() {
        await fetch(`https://demo.vaadin.com/demo-data/1.0/people?count=10`)
          .then(r => r.json())
          .then(async data => {
            this.users = data.result;
          });
      }
    
      render() {
        return html`
          <ul>
            ${this.users.map(
              u =>
                html`
                  <li>${u.lastName}, ${u.firstName} - ${u.email}</li>
                `,
            )}
          </ul>
        `;
      }
    }
    
    window.customElements.define('fetch-lit', FetchLit);
    
    

    你可以在https://stackblitz.com/edit/fetch-lit看到这个例子

    【讨论】:

    • 感谢回复,假设我有多个api,是否可以覆盖静态内容?像这样请看代码部分stackoverflow.com/questions/56285537/…
    • 如果 this.users 还有一个对象但仅覆盖第一个对象的数据?可能吗?如果是这样,请帮助
    • “静态内容”是什么意思?此解决方案覆盖/覆盖this.users 的原始内容。合并只是普通的 JS。查找Array.concat() 或扩展运算符(如将this.users = data.result 更改为this.users = [...this.users, ...data.result])。
    • 如果this.users 有更多对象,fetch 应该只适用于拥有id: Jane 的用户(仅覆盖第一个对象的数据)?可能吗?如果是这样,请帮助 - Senthil 1 小时前
    • 我会(如果有帮助?)快速提醒一下 Polymer 中的异步使用...有趣的部分是此链接中的挑战:blog.mlefree.com/2020/11/i-can-explain-js-async-scope.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多