How to use google app engine with ajax (json)?

前端 未结 2 957
借酒劲吻你
借酒劲吻你 2020-12-30 12:25

How to use google app engine with ajax (json)?

Now I have this but I got this error:

raise ValueError("No JSON object could be decoded&q         


        
相关标签:
2条回答
  • 2020-12-30 13:04

    Your JavaScript is not sending JSON data to App Engine (test=hey&y=99 is a urlencoded string). Your App Engine page is not returning JSON data (Hello, webapp World! will just be received as a naked string).

    0 讨论(0)
  • 2020-12-30 13:13
    1. as long as I know self.request.body wouldn't return anything. There's no argument named 'body' in your query-string, but I might be wrong. So, if it returns something, this something is a STRING. So simplejson.dumps() cannot turn it into a valid JSON.

      If you need a 'list' of all arguments you have sent to the server, use self.request.arguments()

    2. self.response.out.write('Hello, webapp World!') do not send a valid JSON back to client. It sends a string with "application/json" header instead of "plain/text". Try to create a python dictionary. For example:

      my_response = {'ajax_resp':'Hello, webapp World!'}
      json = json.dumps(my_resposne)

      and then

      self.response.headers.add_header('content-type', 'application/json', charset='utf-8')
      self.response.out.write(json)

    3. On the client side I would suggest you to use console.log() (debugging tool) for testing your responses.

      you can just try:

      $.ajax({
         type: 'GET',
         url: '/AJAX', // or your absolute-path
         data : name=totty&age=20,
         dataType : 'json',
         success : function(resp) 
                   {
                   console.info("Ajax Response is there.....");
                   console.log(resp);
                   }
         });

    0 讨论(0)
提交回复
热议问题