json xhr response opens a download file popup window

半腔热情 提交于 2019-12-20 10:34:13

问题


For one of our ajax request (with a .json response) some of our clients have complained that they are seeing a "File Download" prompt asking the user to download the .json response. I am baffled because considering that this is an xhr response, this should never happen. Has anyone seen this?

Thanks


回答1:


For people who are using ASP MVC and have the same problem with IE, use this when returning your response:

return Json(result, "text/plain");

Edit: the standard type is: "application/json", but does not work with IE. using text/html is dangerous.




回答2:


try specifying a MIME type of "text/plain" in the response. or just drop the ".json" extension from the url (try .txt, or .js, for instance)




回答3:


Not sure if you found a solution, but I had a similar problem where IE tried to download any JS responses. To fix it, I had to ensure that format.html appears above format.js in the response block:

def index

  # ...

  respond_to do |format|
    # html must be above js, otherwise IE will try to download the JS
    format.html
    format.js
  end
end

Hope this helps.




回答4:


I am using the QQ ajax file uploader and I found that I needed to set the content type to "text/json" for it to work properly.




回答5:


Drop the .json and set the content type as text/html. IE doesn't know what type of file you are sending it, so it offers to download. It knows what to do with text/html :)



来源:https://stackoverflow.com/questions/1072595/json-xhr-response-opens-a-download-file-popup-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!