Javascript AJAX返回html页面
我已经设置了一个简单的python REST服务器:Javascript AJAX返回html页面,javascript,python,ajax,flask,xmlhttprequest,Javascript,Python,Ajax,Flask,Xmlhttprequest,我已经设置了一个简单的python REST服务器: #!flask/bin/python from flask import Flask, jsonify, abort, make_response, request app = Flask(__name__) data = [ { 'id': 0, 'text': u'working!', } ] @app.route('/data', methods=['GET']) def inde
#!flask/bin/python
from flask import Flask, jsonify, abort, make_response, request
app = Flask(__name__)
data = [
{
'id': 0,
'text': u'working!',
}
]
@app.route('/data', methods=['GET'])
def index():
return jsonify({'data': data})
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
if __name__ == '__main__':
app.run(
debug=True,
host=app.config.get("HOST", "192.168.1.101"),
port=app.config.get("PORT", 5000)
)
然后我做了一个简单的XMLHttpRequest
来检索数据:
doRequest('http://192.168.1.101:5000/data', function(err, response) {
if (err) {
return "error";
} else {
console.log(response);
}
});
}
function doRequest(url, callback) {
var xmlhttp = new XMLHttpRequest(); // create a new request here
xmlhttp.open("GET", url, true); // for async
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status == 200) {
// pass the response to the callback function
callback(null, xmlhttp.responseText);
} else {
// pass the error to the callback function
callback(xmlhttp.statusText);
}
}
}
xmlhttp.send(null);
}
问题
结果有点奇怪。如果我以匿名模式访问网页,它工作正常,我会得到一个JSON文件作为响应,正如预期的那样。如果我在普通浏览器模式下打开它,我会得到一个完整的HTML页面作为响应。为什么会发生这种情况?
我得到了一整页的HTML响应
-你得到了哪一页?@JaromandaX我从哪一个HTTP头发出请求,你发送/获取每个案例?我得到了一整页的HTML响应
-你得到哪一页?@JaromandaX我从哪一个HTTP头发出请求,你发送/获取什么每种情况?