Kalanamith
Kalanamith

Reputation: 20688

Calling a back end URL using javascript channel API

Handler Implementation

class TestHandler(RequestHandler, Jinja2Mixin):
 def get(self):
    channel_id = str(random.randint(1, 10000)) + str(datetime.now())
    chat_token = channel.create_channel(channel_id)
    context = {'channel_id': channel_id, 'chat_token': chat_token}
    return self.render_response('test.html', **context)

 def post(self):
    channel_id = str(random.randint(1, 10000)) + str(datetime.now())
    chat_token = channel.create_channel(channel_id)
    context = {'channel_id': channel_id, 'chat_token': chat_token}
    return self.render_response('test.html', **context)

HTML Implementation

<html>
 <head>
 <script type="text/javascript" language="javascript" src="/static/js/jquery-1.6.1.min.js"></script>
 <script type="text/javascript" language="javascript" src="/static/js/backend.js"></script>
 </head>
<body>
    <form method="post" id="testform" name="testform">
        <br><label name="channel_id" id="channel_id">{{channel_id}}</label>
        <br><label name="chat_token" id="channel_id">{{chat_token}}</label>
        <input type="submit" id="btnsubmit" class="btnsubmit" name="btnsubmit" value="submit" />
    </form>
</body>
</html>

jQuery implementation

 $(document).ready(function () {

    var token =$('#channel_id').val()
    alert(token)

    var channel = new goog.appengine.Channel(token);
    var socket = channel.open();
    socket.onopen = onOpened;

    onOpened = function() {
        connected = true;
        var xhr = new XMLHttpRequest();
        xhr.open('POST','/dashboard/', true);
        xhr.send();
    };

    socket.onmessage = onMessage;
    socket.onerror = onError;
    socket.onclose = onClose;
 });

I want to call a Back end handler. I do not know how to do it. This is what i have done . Can anyone give a help ?

Upvotes: 0

Views: 831

Answers (1)

Moishe Lettvin
Moishe Lettvin

Reputation: 8471

You access a back end with a url like http://instance.backend.appid.appspot.com (see the docs). Since you can't make an XHR to a page like this from an page rendered on http://appid.appspot.com, you have basically two options:

You can marshal the request to your backend via a servlet on your frontend. So you could do something like:

class MarshalServlet(RequestHandler):
  """ This class is part of your frontend. """
  def post(self, instance, backend):
    # generate an urlfetch request to http[s]?://instance.backend.appid.appspot.com
    # and return its result.
    # (left as an exercise for the reader)

# add a "dashboard" handler to your frontend application.
app = webapp.WSGIApplication([('/dashboard/', MarshalServlet),
                               # other servlets etc.
                             ], debug=True)

Or you could use JSONP to do a cross-domain request, which is easy with jQuery's getJSON method:

$.getJSON("http://instance.backend.appid.appspot.com/dashboard/", function() {
  alert("success");
});

It's not clear to me what your /dashboard/ handler does, so I'm not sure if it can/should return JSON or if you care about the headers and so on.

Also note that using the getJSON method won't send along cookies, but you could do that with a marshaling servlet.

Upvotes: 1

Related Questions