简体   繁体   English

在跨域Ajax调用中获取标题信息

[英]Get Header info in cross domain ajax call

I am trying to validate an image URL if it still holds the image or not by making ajax call to that URL. 我正在尝试通过对该URL进行ajax调用来验证图像URL是否仍然保存该图像。 One problem is the image server is on a different domain so I am using crossDomain:true attribute in my ajax call. 一个问题是图像服务器在另一个域上,因此我在ajax调用中使用crossDomain:true属性。 Here is what I have tried so far: 到目前为止,这是我尝试过的:

function checkFunc(){
  $.ajax({ 
        url: 'https://www.google.co.in/images/srpr/logo11w.png', 
        dataType: 'image/png',
        crossDomain: true,
        timeout: 5000,
        error: function(e, status, settings){
            alert('ERROR: '+JSON.stringify(e));
        },
        success: function( e, xhr, settings ) {    
            alert('SUCCESS: '+JSON.stringify(e));
        }   
    });
}

But its not working. 但是它不起作用。 Also a concern is the images are not confined to a single format, ie the image can be png/jpg/gif or any other so I need to have a broader dataType to accept any kind of image. 还有一个问题是图像不限于单一格式,即图像可以是png / jpg / gif或任何其他格式,因此我需要具有更大的dataType才能接受任何类型的图像。

I have also tried using jsonp, but that gives me error as "Refused to execute script from because its Mime type(image/jpeg) is not executable. 我也尝试过使用jsonp,但这给我的错误是“拒绝执行脚本,因为它的Mime类型(image / jpeg)无法执行。

Edit: I cannot run server script from my ajax function which in turn calls the cross domain page, as in php getcontents 编辑:我无法从ajax函数运行服务器脚本,而ajax函数又调用跨域页面,如php getcontents

You can do one thing for that just need to set Access-Control-Allow-Origin & Access-Control-Allow-Headers 您只需为此设置Access-Control-Allow-Origin和Access-Control-Allow-Headers就可以做一件事

 <add name="Access-Control-Allow-Origin" value="*" />
 <add name="Access-Control-Allow-Headers" value="Content-Type" />

If you want to allow only for specific domain , you can do that with specific value of domain instead of * value 如果只允许特定域,则可以使用特定域值代替*值来实现

You have to set up the image server to allow cross-origin requests. 您必须设置图像服务器以允许跨域请求。 To do that you need to set the a Header on the server holding the images like this: 为此,您需要在保存图像的服务器上设置一个Header,如下所示:

Header set Access-Control-Allow-Origin "*"

You can replace the * with your specific domain, in fact you should, otherwise anyone can do a cross-origin request on your image server. 您实际上可以将*替换为您的特定域,否则任何人都可以在图像服务器上进行跨域请求。

  • The 'xhrFields' property sets additional fields on the XMLHttpRequest. “ xhrFields”属性在XMLHttpRequest上设置其他字段。
  • This can be used to set the 'withCredentials' property. 这可以用来设置'withCredentials'属性。
    Set the value to 'true' if you'd like to pass cookies to the server. 如果您想将cookie传递到服务器,请将值设置为“ true”。 If this is enabled, your server must respond with the header 'Access-Control-Allow-Credentials: true'. 如果启用此功能,则服务器必须使用标头“ Access-Control-Allow-Credentials:true”进行响应。

It may help to you 可能对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM