Creating a JSON response using Django and Python

后端 未结 15 2363
温柔的废话
温柔的废话 2020-11-22 06:02

I\'m trying to convert a server side Ajax response script into a Django HttpResponse, but apparently it\'s not working.

This is the server-side script:



        
15条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 06:36

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

    Code Javascript with JQuery:

    $.ajax({
        url: '/ajax',
        dataType : 'json',
        cache: false,
        success: function(data) {
            alert('Load was performed.'+data.ajax_resp);
        }
    });
    

    Code Python

    class Ajax(webapp2.RequestHandler):
        def get(self):
            my_response = {'ajax_resp':'Hello, webapp World!'}
            datos = json.dumps(my_response)
    
            self.response.headers.add_header('content-type', 'application/json', charset='utf-8')
            self.response.out.write(datos)
    

提交回复
热议问题