如何使用extJS发布JSON数据

20

我对extJS和json都有点新手。使用extJS POST json数据的最简单方法是什么?我不太关心任何GUI功能,只想使用框架发送一些示例数据。

5个回答

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

23
这将以URL编码的形式发布数据...也就是说,POST缓冲区将是foo=bar。如果您将“params”替换为“jsonData”,它将发布原始JSON,因此POST缓冲区将是{"foo":"bar"} - SBUJOLD
在ExtJS 4.1中,您可以使用jsonData成员。 - Chris

20

以下内容将被识别为 '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
});

2
您可以使用 method: 'POST' / 'GET' 参数:http://docs.sencha.com/extjs/4.1.3/#!/api/Ext.Ajax-property-method - efirat

6

我想补充一下:

//
//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;

3
这里发布的示例展示了基本思路。有关所有可配置选项的完整详细信息,请参见Ext.Ajax文档

链接已经失效,必须导航到EXT.Ajax部分。 - oden

0
代码片段:
 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接