[英]ajax code returning ProgressEvent object rather than my template data
I'm trying to improve the UX on a few pages by adding some ajax. 我试图通过添加一些Ajax来改善几页上的UX。 I have an ajax request:
我有一个ajax请求:
var xhr = new XMLHttpRequest();
xhr.open('POST', '/search/', true);
xhr.onload = function(data){
document.getElementById("search-results-container").innerHTML = data;
}
xhr.setRequestHeader("X-CSRFToken", csrftoken);
xhr.send(form_data);
This isn't giving me the rendered template from my django development server. 这不是给我django开发服务器提供的渲染模板。 Instead I get
[object ProgressEvent]
in my #search-results-container
div. 相反,我在
#search-results-container
div中获得了[object ProgressEvent]
。 The django view renders correctly if I submit the request synchronously. 如果我同步提交请求,django视图将正确呈现。
I'm probably completely misunderstanding the spec , but aren't I supposed to get the template data + http headers straight back from the server? 我可能完全误解了规范 ,但是我不应该直接从服务器获取模板数据+ http标头吗? What have I done wrong here?
我在这里做错了什么?
The event handlers for XHR events are passed event objects. XHR事件的事件处理程序是传递的事件对象。 Newer browsers support the
ProgressEvent
API. 较新的浏览器支持
ProgressEvent
API。 Those won't give you the data from the request, however; 但是,那些不会提供请求中的数据。 for that you'll need to retain access to the XHR object itself.
为此,您需要保留对XHR对象本身的访问权限。 The
.responseText
and (if appropriate) the .responseXML
properties will contain your response content once the HTTP request has actually completed (which, in your "load" handler, it will have). HTTP请求实际完成后(在“加载”处理程序中将具有)
.responseText
和(如果适用) .responseXML
属性将包含您的响应内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.