- 服务器端代码:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route("/ajax/")
def hello():
return "bangbang({name:'hello'})", 200, {'Content-Type': 'application/x-javascript'}
if __name__ == '__main__':
app.run(port=5555)
服务器端需要注意的:
bangbang为callback,callback(json数据),类型需为x-javascript类型;
- index.html
<!DOCTYPE html>
<html>
<head>
<title>index</title>
<script type="text/javascript" src="/static/jquery-1.12.3.min.js"></script>
<input type=button id="id-dianji" value="woca" onclick="enter()">
<script type="text/javascript">
function enter(){
$.ajax({
url:"http://127.0.0.1:5555/ajax/",
dataType:"jsonp",
cache:false,
async:true,
type:"GET",
jsonpCallback:"bangbang",
success:function(args){
console.log(args);
}
})
}
function bangbang(args){
alert(args);
}
</script>
</head>
<body>
</body>
</html>