Javascript AJAX请求不工作GAE

Javascript AJAX请求不工作GAE,javascript,python,ajax,google-app-engine,python-2.7,Javascript,Python,Ajax,Google App Engine,Python 2.7,我正在关注来自的tut,但我无法让ajax工作,我也检查了chromes控制台中的标题,但我也没有看到text/json标题!你能帮我一下吗?我想不出来。 提前谢谢 main.py class Main(webapp2.RequestHandler): def get(self): if self.request.get('fmt') == 'json': data = {'name' : 'sam', 'age': 25} self.response.h

我正在关注来自的tut,但我无法让ajax工作,我也检查了chromes控制台中的标题,但我也没有看到text/json标题!你能帮我一下吗?我想不出来。 提前谢谢

main.py

class Main(webapp2.RequestHandler):
def get(self):
    if self.request.get('fmt') == 'json':
        data = {'name' : 'sam', 'age': 25}
        self.response.headers['content-type'] = 'text/json'
        self.response.write(json.dumps(data))
        return
    self.templateValues = {}
    self.templateValues['title'] = 'AJAX JSON'
    template = jinja_environment.get_template('index.html')
    self.response.write(template.render(self.templateValues))

app = webapp2.WSGIApplication([('/.*', Main),], debug=True)
index.html

<input type ="button" id="getitbutton" value="click button"/>
<div id= "result">
</div>

js脚本

<script  type="text/javascript" >
function showData(data){
    console.log(data.name);
    $('#result').html(data.name)
}
function handleclick(e){
    $.ajax('/',{
        type: 'GET',
        data: {
            fmt: 'json'
        }
        success: showData
    });
}
$(document).ready(function(){
    $('#getitbutton').on('click', handleclick);
});
</script>

函数showData(数据){
console.log(data.name);
$('#result').html(data.name)
}
函数handleclick(e){
$.ajax(“/”{
键入:“GET”,
数据:{
fmt:'json'
}
成功:showData
});
}
$(文档).ready(函数(){
$('getitbutton')。在('click',handleclick)上;
});

当前浏览器存在一些问题,尤其是IE,它不发送text/json内容类型头。所以我学会了不依赖标题

相反,我的解决方案如下:

js ajax函数:

function ajax(url,obj,callback){
    var xhr=new XMLHttpRequest;
    xhr.onreadystatechange=function(){
        if(this.readyState==4){
            if(callback){
                callback(this.responseText);
            }
        }
    }
    xhr.open("POST",url);
    xhr.send(JSON.stringify(obj));
}
然后在服务器端,我直接从请求正文中读取(很抱歉Go代码,但我认为您也可以从Python运行时获取请求正文):

希望这有帮助

// read the request body
body, _ := ioutil.ReadAll(httpRequest.Body)

// parse the json payload
var user struct {
        Email    string
        Password string
}
json.Unmarshal([]byte(body), &user)