Showing posts with label GoogleAppEngine. Show all posts
Showing posts with label GoogleAppEngine. Show all posts

Thursday, February 3, 2011

How to get data from Google App Engine with XMLHttpRequest on PhoneGap

There is a cross domain issue when we implement this program.
Follow the code disolves it.

javascript side:
var url = request; // ex)"http://localhost:8080"
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function(){
if ( xhr.readyState == 4 ) {
if ( xhr.status == 200 ) {
var response = JSON.parse(xhr.responseText);
$.each(response,function(i,r){
alert(r.content);
});
alert("get success!");
} else {
alert(xhr.responseText);
alert("lose");
}
}
};
xhr.send(null);
Google App Engine side:
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.headers['Access-Control-Allow-Headers'] = '*'
self.response.headers['Access-Control-Allow-Methods'] = 'GET'
self.response.headers['Content-Type'] = 'text/plain;charset=UTF-8'
tweets = db.GqlQuery('SELECT * FROM Tweet')
results = []
for tweet in tweets:
result = {}
result['content'] = tweet.content
results.append(result)
self.response.out.write(simplejson.dumps(results))

reference:
XMLHttpRequest Cross-domain issue
As soon as you sent the code, I have tested it and it did not work in my Chrome, Firefox or Android device. So, I came home last night and tested your code on Safari and my iPhone app, it works without any issues, actually my original code also worked without any issues in Safari and iPhone. 

Saturday, January 29, 2011

How to get data from Google App Engine for python with jQuery

GAE side:
import simplejson
...
class HistoryHandler(webapp.RequestHandler):
def get(self):
viewerid = self.request.get('viewerid')
query = "SELECT * FROM dbmodel where viewerid = '" + viewerid + "'"
datas = db.GqlQuery(query)
result = []
for data in datas:
r = {}
r["key"] = data.key().id()
r["unreadflag"] = data.unreadflag
result.append(r)
self.response.out.write(simplejson.dumps(result))
jQuery side:
    $.get('http://xxxxxxxx.appspot.com/historydata signed', {viewerid : viewerid}, function(results) {
      if(results.length == 0){
      ...
      }
      else{
              //when results is an object, this "i" is a hash key.
              //when results is an array, this "i" is an index.
     $.each(results, function(i, r) {
     alert(r.unreadflag);
     });
      }
    }, 'json');
P.S.
My English is not good enough.
If there is a mistake and an anxious point,teach me softly.

How to post data to Google App Engine for python with jQuery

jQuery side:
$.post(
"http://xxxxxxxxxxxxxxxxx.appspot.com/introdata",
{"viewerid": viewerid, "fromid": from, "toid": to, "introreason": reason},
function(data, status) {
$("#oktext").css('font-size', '12px').css("font-weight", "bold").css("color", "red").fadeIn(2000);
$("#oktext").fadeOut(5000);
}
);
GAE side:

from django.utils import simplejson
...

class AddHandler(webapp.RequestHandler):
       ...
def post(self):
viewerid = self.request.get('viewerid')
data = introData(viewerid=viewerid)
data.fromid = self.request.get('fromid')
data.toid = self.request.get('toid')
data.introreason = self.request.get('introreason')
data.unreadflag = '1'
data.put()
...
def main():
application = webapp.WSGIApplication(
[
('/introdata',AddHandler),
...
],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
main()