0

Here is my JS code:

    <script>
         $("#comments").click(function(event) {
            $.ajax({
               type: "GET",
               url: '/localhost:8080/comment',
               data: JSON.stringify(
                     {
                      'name': 'anon',
                      'subject': 'MY COMMENTS',
                     }),
               contentType: 'application/json',
               success: function(data,textStatus, jqXHR) {
                  console.log('POST response: ');
                  console.log(data);
              }
            });   
      });
   </script>

and here is my Python Code:

class Guestbook(webapp2.RequestHandler):

  def get(self):

    Jguest_data = json.loads(self.request.body) 
    return self.response.out.write(json.dumps(Jguest_data))

I got the error 404 Resource not found. After digging around there is some issues with localhost . so I tried with JSONP as follows:

<script>
     $("#comments").click(function(event) {      
        $.ajax({ // ajax call starts
          url: "localhost:8080/comment", // 
          type: "GET",
          data:  JSON.stringify(
                {
                  'name': 'anon',
                  'subject': 'MY COMMENTS',
                }),
          dataType: "jsonp", // Choosing a JSON datatype
          success: function(data,textStatus,jqXHR) 
          {
              console.log('POST response: ');
              console.log(data);
          }
        });       
  });
 </script>

That still does not work... I get "No JSON object could be decoded" Error. I tried replacing JSON.loads with JSON.load... and that still errors out...

Can someone please let me know what the issue is?

Thanks a mil in advance

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
user1570124
  • 31
  • 1
  • 1
  • Apparently, neither is your Caps Lock key. – Niet the Dark Absol Jan 18 '13 at 04:26
  • 1
    There should be no problems testing a json/ajax call from the dev_appserver.py webserver to itself, since it would not be a cross-domain call. Are you sure the `$.ajax` url is correct? Try `http://localhost:8080/comment` or `/comment`. The last one will change depending on the url of the page that runs the script. – Martín Valdés de León Jan 18 '13 at 05:25
  • See [this SO answer](http://stackoverflow.com/questions/5106306/how-to-use-google-app-engine-with-ajax-json?rq=1) – doru Jan 18 '13 at 09:50

2 Answers2

0

when you do a GET request with ajax you are using url parameters.
so there is no body in the request.
make a POST and change your get() to post()

aschmid00
  • 7,038
  • 2
  • 47
  • 66
0

You should edit your app.yaml file if there is one in your project, and if there is not one, add one.

Add this code to your file (app.yaml).

-url: /comment
  script: <url-to-the-server-side-script>

You can see a more complete app.yaml documentation for use with python here.

https://cloud.google.com/appengine/docs/python/config/appref