【问题标题】:How to post json data with extJS如何使用 extJS 发布 json 数据
【发布时间】:2011-02-24 10:41:11
【问题描述】:

我对 extJS 和 json 有点陌生。使用 extJS 发布 json 数据最简单的方法是什么?我对任何 GUI 功能都不感兴趣,只是使用框架发送一些示例数据。

【问题讨论】:

    标签: html json post extjs


    【解决方案1】:

    代码片段:

     Ext.Ajax.request({
        url: "https://reqres.in/api/users",
        success: function (response) {
            Ext.Msg.alert("success", response.responseText);
        },
        failure: function () {
            Ext.Msg.alert("failure", "failed to load")
        },
        params: {
            "name": "morpheus",
            "job": "leader"
        }
    });
    

    小提琴:https://fiddle.sencha.com/#view/editor&fiddle/28h1

    【讨论】:

      【解决方案2】:

      此处发布的示例显示了基本思想。有关所有可配置选项的完整详细信息,请参阅Ext.Ajax docs

      【讨论】:

      • 链接已损坏,必须导航到 EXT.Ajax 部分
      【解决方案3】:

      以下将标识为“POST”请求

       Ext.Ajax.request({
             url: 'foo.php',    // where you wanna post
             success: passFn,   // function called on success
             failure: failFn,
             jsonData: { foo: 'bar' }  // your json data
          });
      

      以下将标识为“GET”请求

      Ext.Ajax.request({
         url: 'foo.php',    // where you wanna make the get request
         success: passFn,   // function called on success
         failure: failFn,
         params: { foo: 'bar' }  // your json data
      });
      

      【讨论】:

      【解决方案4】:

      只是为了加我的两分钱:

      //
      //Encoding to JSON:
      //
      var myObj = {
        visit: "http://thecodeabode.blogspot.com/"
      };
      var jsonStr = Ext.encode(myObj);
      
      
      //
      // Decoding from JSON
      //
      var myObjCopy = Ext.decode(jsonStr);
      document.location.href = myObj.visit;
      

      【讨论】:

        【解决方案5】:
        Ext.Ajax.request({
           url: 'foo.php',    // where you wanna post
           success: passFn,   // function called on success
           failure: failFn,
           params: { foo: 'bar' }  // your json data
        });
        

        【讨论】:

        • 这将像数据一样发布 URLencoded ... IOW,POST 缓冲区将是 foo=bar。如果您将params 替换为jsonData,它将发布原始JSON,因此POST 缓冲区将为{"foo":"bar"}
        • 在 ExtJS 4.1 中你可以使用 jsonData 成员。
        猜你喜欢
        • 2011-01-13
        • 2021-03-17
        • 2015-05-05
        • 2018-05-28
        • 2011-09-09
        • 1970-01-01
        相关资源
        最近更新 更多