如何防止浏览器请求资产来自ajax响应 [英] how to prevent the browser from requesting the assets come from the ajax response

查看:59
本文介绍了如何防止浏览器请求资产来自ajax响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是请考虑我是网络上的新手.

It might be a silly question, but please consider I'm very newbie in web.

我使用jquery $.ajax 创建了一个ajax,然后将响应放入当前页面的< div> 中,一旦执行此操作,浏览器提出对ajax响应中存在的所有资产的请求.

I made an ajax with jquery $.ajax , then I put the response in a <div> on the current page, and once I do this the browser makes requests for all the assets exist in the ajax response.

例如:这是来自ajax的简单响应

for example : this is the plain response came from the ajax

   this is a plain text response from my php server
   <div>
   <p> this is the important response,(some value)</p>
   <image src="NOT_important_image.png"></image>
  </div>

如何防止客户端上的浏览器请求图像?

我试图使PHP响应为 header('Content-Type:text/plain; charset = utf-8'); 希望浏览器将响应作为纯文本而不是html来处理,但仍在请求图像.

I tried to make the PHP response header('Content-Type: text/plain; charset=utf-8'); hoping the browser deal with the response as a plain text not an html, but it still requesting the image.

我可以从服务器PHP中删除图像,但是我必须在客户端使用某些客户端工具(例如 javascript )

I can remove the image from the server PHP , but I have to do this on the client side with some client tools (e.g: javascript)

推荐答案

这与PHP或从服务器返回的标头无关,但是当您将此html代码段包含到DOM中时,浏览器将尝试呈现它

This has nothing to do with PHP or headers returned from the server, but when you include this html-snippet into the DOM, the browser will try to render it.

在您的情况下,如果它包含img-tag并且设置了src-attribute,它将尝试获取内容,以便将其显示给用户.

In your case, if it includes an img-tag and the src-attribute is set, it will try to fetch the contents, to be able to show it to the user.

从html中删除img标签,或改为将数据作为JSON对象发送,当您希望在页面上呈现html时,可以使用该对象来填充正确的html.

Remove the img-tag from the html, or send the data as a JSON-object instead, which you can use to populate the correct html with when you would like to render it on the page.

这篇关于如何防止浏览器请求资产来自ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆