简体   繁体   中英

Sencha touch create update delete by using jersey

I am new for sencha touch . I need to create, delete, update a member record throughout jersey java. Here is my code

view.js

    Ext.define('bluebutton.view.BlueButton.testing', {
    extend: 'Ext.form.Panel',
    xtype: 'testing',
       requires: [

    'bluebutton.view.BlueButton.TransactionList',
    'bluebutton.view.BlueButton.MemberPopUp',
     'bluebutton.view.BlueButton.MemberDetail',
     'bluebutton.store.BlueButton.MemberList',

    ],
    config: {
     id:'register',
        items :[

              {
                    xtype: 'textfield',
                    name: 'name',
                    label: 'Name'
                },
                {
                    xtype: 'emailfield',
                    name: 'email',
                    label: 'Email'
                },

                 {
                    xtype: 'button',
                    text: 'Send',
                    handler: function(button) {
                        var form = Ext.getCmp('register');
                          values = form.getValues();



        var myStore= new Ext.data.Store({
            model: 'bluebutton.model.BlueButton.MemberList'
        });

            var newModel = Ext.ModelMgr.create({ 'memberId': 1,
                        'name': 2,
                        'imgUrl': 3
                    }, 'bluebutton.model.BlueButton.MemberList');

            myStore.add(newModel);

            myStore.sync();


                    }
                }


        ],


   }

});

Model.js

Ext.define('bluebutton.model.BlueButton.MemberList', {
    extend: 'Ext.data.Model',
    config: {
        idProperty: 'memberModel',
        fields: [
            { name: 'memberId' },
            { name: 'name' },
            { name: 'imgUrl' },
            { name: 'age' },
            { name: 'address' },
            { name: 'pointAvalaible' },
            { name: 'lastVisited' },
        ],


  proxy: {
            type: 'ajax',
            actionMethods: {
                create : 'POST',
                read   : 'POST', // by default GET
                update : 'POST',
                destroy: 'POST'
            },
            url: 'http://localhost:8080/RESTFulExample/rest/json/metallica/testingj'
        },


    }

});

Store.js

Ext.define('bluebutton.store.BlueButton.MemberList', {
    extend: 'Ext.data.Store',
   requires: [
              'bluebutton.model.BlueButton.MemberList'
    ],

    config: {
        grouper: {
            groupFn: function (record) {
                return record.get('name')[0];
            }
        },


           model :'bluebutton.model.BlueButton.MemberList',
           storeId :'memberStore',
           autoLoad:false,
           pageSize: 5,
           clearOnPageLoad: false, 

        data: [


        ]
    }
});

Jersey.java

     @POST
     @Path("/testingj")
//   @Consumes("application/x-www-form-urlencoded")
     @Consumes(MediaType.APPLICATION_JSON)
     public Response too(MultivaluedMap<String, String> form) {
     return Response.status(201).entity(form).build();
 }

But I keep getting this error

XMLHttpRequest cannot load localhost:8080/RESTFulExample/rest/json/metallica/testingj?_dc=1358408853016. 
Origin localhost is not allowed by Access-Control-Allow-Origin.

Please guide me solution.

You are getting this response because of browser's security constraint for cross origin requests, which means from javascript you can call only those urls which are in same domain. Since your service is running on 8080 post so browser consider it from other domain.

There are various ways to do cross domain request, out of which you should read about:

  1. JsonP
  2. Enabling CORS on server

But since mobile's security constraints are different from desktop's browser constraints so the same thing might work in mobile even though it doesn't work in browser. For more details check this out : Loading store data with rest proxy from server in Sencha Touch 2

In your case I would recommend you to add JsonP support to your services and use JsonP proxy instead of AJAX & REST proxy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM