Sencha touch XML response to JSON

2019-07-21 04:07发布

问题:

I have recently started a project in Sencha touch with existing Web-services. Being very new to the technology, I am facing certain issues in accomplishing some functionality.

Problem I have to call login service and the request goes like:

http://domain.sub.com/Service.asmx/LoginService?body={"Username":"maj@smaj.com","Password":"p12345","Token":122112321123212123,"Method":"Login","LabId":"(null)","Hash":"fr3f33f3334348u8yy8hfuhdu8bdy7y89u89x8998c89789c87d78r9","DeviceType":"iPhone Simulator","DeviceId":"91BF3299-A94C-5AD3-9C35-A5C9BBBB6AA8","ApplicationType":"iPhone","Id":"998390494"}

but the response is coming in XML format as: RESPONSE:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://domain2.sub2.com/MobileWebService/">{"Id":"null","Message":"Logged In.","Status":true,"LoggedIn":true}</string>

I have to parse this xml to json to get : {"Id":"null","Message":"Logged In.","Status":true,"LoggedIn":true} out of the response.

then use the Status, LoggedIn and Id to verify the login.

My Idea I am not sure whether its right, I am trying to create two stores, xmlStore and JsonStore.

??

  • How will I store the xml response inside a string.
  • How Will I pass this string to Json Store (at the place of url?)

I may sound very naive to this, but this is my problem ;)

Please guide.

EDIT:

I realized tha I am diving cross domain request. is that what is causing problems or confusion. How to deal with it suppose I did not had cross domain requests?

回答1:

If you are doing cross domain requests use scripttag in your proxy in place of ajax. Here is a json example

ex.

mApp.stores.onlineStore = new Ext.data.Store({
 model: 'XPosts',
 proxy: {
           type: 'scripttag',
           url : 'http://domain.com/data.json',
           reader: new Ext.data.JsonReader({
               root: 'pages'
           }),
            timeout: 3000,
        listeners: {
            exception:function () {
                console.log("onlineStore: failed");
            },
                success: function(){
                    console.log("onlineStore: success");
                }
            }
       },
    autoLoad: true
 });

Offline store:

mApp.stores.offlineStore = new Ext.data.Store({
    model: 'XPosts',
    proxy: {
        type: 'localstorage',
        id: 'appdata',
        reader: new Ext.data.JsonReader({
            root: 'pages'
        })
    },
    autoLoad: true
});

Then, in your launch :

this.stores.onlineStore.addListener('load', function () {
       console.log("onlineStore: online");
       mApp.stores.offlineStore.proxy.clear();
        console.log("offlineStore: cleared");
        this.each(function (record) {
            console.log("offlineStore: adding record");
           mApp.stores.offlineStore.add(record.data)[0];
       });
       mApp.stores.offlineStore.sync();
        console.log("offlineStore: synced");
        mApp.stores.offlineStore.load();
    });
    this.stores.onlineStore.load();

May have some bugs so beforewarned!