简体   繁体   English

返回ProgressEvent对象而不是我的模板数据的ajax代码

[英]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.

相关问题 返回 API 响应而不是数据 - Returning API response rather than data Javascript Ajax get请求返回json文件readystate 1而不是数据 - Javascript ajax get request returning json file readystate 1 rather than data 为什么将ProgressEvent对象传递给JavaScript AJAX错误事件处理程序? - Why is a ProgressEvent object passed to a JavaScript AJAX error event handler? Axios post 方法正在发送一个对象,其中我的数据作为键和空值,而不是整个数据对象本身 - Axios post method is sending a object with my data as the key and empty value rather than the whole data object itself 为什么我的代码落后 2 个刻度(而不是 1 个)? - Why is my code is 2 ticks behind (rather than 1)? 我的AJAX响应正在下载而不是加载到成功数据中 - My AJAX responses are getting downloaded rather than getting loaded in success data 将一个PHP对象从ajax文件返回到我的javascript代码 - returning a PHP object from an ajax file to my javascript code 函数返回函数对象而不是原型值 - Function returning function object rather than prototype value 是否可以将Template7代码放在单独的文件中而不是html中 - Is that possible to put Template7 code in a separate file rather than in html Javascript'this'关键字返回href属性,而不是锚标记上的Object - Javascript 'this' keyword returning href attribute rather than Object on anchor tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM