Chris Bunch
Chris Bunch

Reputation: 89823

Recommended way to have JavaScript interact with App Engine APIs

I have a Google App Engine app that uses JavaScript for some fairly standard functionality. One button, the 'save' button, should save a textfield into an entry in the datastore. However, that save button is all JavaScript, which is trying to do the following right now:

  1. prompt user for filename (the key_name in the datastore that I'll save it under)
  2. save it in the datastore
  3. flash a message letting the user know we saved their data (presuming it saved without error)

Any ideas on what the correct way to do this within App Engine is? I could have the JS access a certain URL, but I don't know if it would funnel the user's authentication info over correctly or if any arbitrary user could call that URL and read / write the datastore. Also, as I'm a bit new to JavaScript / jQuery over App Engine, I think I may be just solving this problem wrong, so I'm open to other design choices.

Upvotes: 1

Views: 187

Answers (3)

Uri
Uri

Reputation: 26966

Jquery (a popular Javascript library) has a convenient ajax function. You can use it to send a request to your API.

For example:

$.ajax({
    url: '/api_address',
    dataType: 'json',
    data: {
        arg0: JSON.stringify(some_variable),
        arg1: JSON.stringify(another_variable),
    },
    success: callback
};

Once received on the other side, the server can check the cookies that came with this request for authentication.

As the other guys said, protoRPC is a new API in app engine that makes the server side API easier to write.

Upvotes: 0

Sudhir Jonathan
Sudhir Jonathan

Reputation: 17516

Also check out the upcoming ProtoRPC library that will come out in 1.5.1. Makes handling Javascript clients a breeze.

Upvotes: 0

Nick Johnson
Nick Johnson

Reputation: 101149

Implement an API in App Engine (a simple restful one would suffice), and interact with it in Javascript, using XMLHTTPRequest. Any requests for a logged in user will include the login cookies, so you'll be able to check for authentication the same way as for any other request.

Upvotes: 2

Related Questions