What is the proper way of uploading the records via proxy type 'jsonp'? I'm trying to sync() the store, with proxy type "jsonp', but I get error message.
This is the model:
Ext.define("Sencha.model.User", {
extend:"Ext.data.Model",
//idProperty:"",
config:{
fields:[
'userID',
'userName',
'userEmail'
],
proxy: {
type: 'jsonp',
create : 'http://domainname.com/users.php?action=insert',
read : 'http://domainname.com/users.php?action=fetchAll',
update : 'http://domainname.com/users.php?action=update',
destroy : 'http://domainname.com/users.php?action=delete'
},
callbackKey: 'callback',
reader: {
type: 'json',
rootProperty: 'Users',
successProperty: 'success',
messageProperty: 'message'
},
writer: {
type: 'json',
writeAllFields: false,
encode: true
}
}
}
});
The store:
Ext.define("Sencha.store.Users", {
extend:"Ext.data.Store",
config:{
model:"Sencha.model.User",
remoteFilter:false,
remoteSort:true,
autoLoad:true,
}
}
});
The store is updated:
Ext.getStore('Users').set('userName', 'Tom');
Now I'd like to update the record in database:
Ext.getStore('Objects').sync();
but I get the error: Uncaught Error: [ERROR][Ext.data.proxy.Server#create] JsonP proxies can only be used to read data.
How can I update the record data - upload it to database via proxy?
CRUD is an acronym that comes from the world of computer programming and refers to the four functions that are considered necessary to implement a persistent storage application: create, read, update and delete.
September 21, 2022. CRUD is the acronym for CREATE, READ, UPDATE and DELETE. These terms describe the four essential operations for creating and managing persistent data elements, mainly in relational and NoSQL databases.
CRUD stands for "Create, Read, Update, and Delete," which are the four basic database operations. Many HTTP services also model CRUD operations through REST or REST-like APIs. In this tutorial, you will build a very simple web API to manage a list of products.
In modern software programming and development, CRUD has exceeded its start as a function and lent itself to design principles to applications like SQL, DDS, and HTTP protocol.
You are dealing with CORS (Cross Origin Resource Sharing) By default in all brwsers this is respected and all web servers are by default set to not allow CORS requests. More about CORS here
If you are web developer, and have web server but you need access some external company API from javascript, easiest way is to setup your server to act as web proxy. Below are steps for some servers
(Guru readers, feel free to add here more server configs since I proclamed this as wiki)
Open your virtual host file and add those lines (enable mod proxy foirst - further reading here)
ProxyPass /proxy http://domainname.com/
ProxyPassReverse /proxy http://domainname.com/
If you are using NGIX for your app configarion add folowing lines
location /proxy {
proxy_pass http://domainname.com/;
proxy_set_header X-Real-IP $remote_addr; <-- maybe good to set
}
Further reading on this link.
If you are using IIS server
the config is a bit more complex then those above but you can find all about it here
For all above examples instead of using limited JSONP capabilities now you can use Ajax and JSON from response as you are serving that API on your server.
I can say one thing at the biggining. You will either take blue pill or Red pill :) I'm joing but there are two ways. One involes having, again, your own server with configuration as above and second is to dirty your hands with Phonegap native plugin.
Requires that you have your own web server. You will need above configs for mod_proxy so you can hide real service behind your service and proxy all HTTP requests from your phonegap app. You also have to include CORS (Cross Origin Resource Sharing) headers in response which is returned to phonegap app. Also consider to secure this with authentication since you are exposing content to world. Your phonegap app shold authenticate to your webservice, at least, trough basic HTTP auth over HTTPS.
Follow this steps to complete setup:
On apache server, first enable modules "headers"
$ a2enmod headers
in virtual host file before or after proxy configuration add following:
ProxyPass /proxy http://domainname.com/
ProxyPassReverse /proxy http://domainname.com/
# CORS allow to all
Header set Access-Control-Allow-Origin *
# Set basic authentication
<Location /proxy>
AuthType Basic
AuthName "Restricted Area"
AuthBasicProvider file
AuthUserFile /usr/local/apache/passwd/passwords
Require valid-user # setup user/password in file above using htpasswd
</Location>
In phonegap (Sencha Touch), for ajax request setup username and password like buffer.SlowBuffer();
You'll need first method to pack auth header
function make_base_auth(user, password) {
var tok = user + ':' + pass;
var hash = Base64.encode(tok);
return "Basic " + hash;
}
Then in your proxy set header like this
headers : { Authorization : make_base_auth("some_username", "some_password") } // I know this is readable by other by you get the point.
To CORS-enable Microsoft IIS6, perform the following steps:
Optional, set basic auth, it's straight forward process.
Also consider to check documentation which is mentioned above on how to set proxy and then amend that web.config file and add folowing
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
In location append following
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 200;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
Since it is to long to fit in this box, I will provide link only to one blog where all is exlained properly
The link
Well it involves some native coding at least you will need phonegap plugin phonegap-proxy which you can find here But I'd avoid "native" since point of phonegap is to have multi platform app using single code... Oh, if you want to do your "special" plugin writing native code here is good example how to do same thing for facebook API
It is now all up to you to decide which approach you will take ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With