2010-05-26 14 views
19

Sono un po 'un newb con entrambi extJS e json. Qual è la via più indolore per inviare i dati JSON utilizzando extJS? Non mi interessa davvero nessuna caratteristica della GUI, ma semplicemente utilizzo il framework per inviare alcuni dati di esempio.Come inviare dati json con extJS

risposta

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

Oh, wow, che era molto più facile di quanto mi aspettassi. Grazie!!!!! – maximus

+22

Questo invierà URLencoded come dati ... IOW, il buffer POST sarà foo = bar. Se si sostituisce 'params' per' jsonData', verrà inserito raw JSON, quindi il buffer POST sarà '{" foo ":" bar "}' – SBUJOLD

+0

In ExtJS 4.1 è possibile utilizzare il membro jsonData. – Chris

3

Gli esempi pubblicati qui mostrano l'idea di base. Per i dettagli completi su tutte le opzioni configurabili, vedere Ext.Ajax docs.

+0

Il collegamento è interrotto, è necessario spostarsi sulla sezione EXT.Ajax – oden

6

Giusto per aggiungere i miei due centesimi:

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

Di seguito identificherà come richiesta '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 
    }); 

Di seguito vi identificano come 'GET' richiesta

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 
}); 
+1

è anche possibile utilizzare il metodo ': parametro POST '/' GET'': http://docs.sencha.com/extjs/4.1.3/#! /api/Ext.Ajax-property-method – efirat

0

Codice Snippet:

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

Fiddle: https://fiddle.sencha.com/#view/editor&fiddle/28h1

Problemi correlati