jQuery等同于PHP的file_exists()? [英] jQuery equivalent of PHP's file_exists()?

查看:83
本文介绍了jQuery等同于PHP的file_exists()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码段中,从我的jQuery设置中,我需要检查图像文件是否实际存在,如果不存在,我想替换默认图像.目前,如果文件不存在,我会得到一个损坏的图像占位符...

In the code snippet below, from my jQuery setup, I need to check if the image file actually exists and if not, I'd like to substitute a default image. Currently if the file does not exist, I just get a broken image placeholder...

$('#myTheme').change
(
    function() 
    {
    var myImage = $('#myTheme :selected').text();
    $('.selectedImage img').attr('src','../wp-content/themes/myTheme/styles/'+myImage+'/screenshot.jpg');
    //if screenshot.jpg does not exist, use "../../default.jpg" instead
    }
);

推荐答案

听起来您需要访问HTTP响应标头.这是我在互联网上找到的一段代码.

It sounds like you need access to the HTTP response headers. Here's a piece of code I found on the Internet.

来源: http://www.vfstech.com/?cat=1

$(document).ready(function() {
   var ajaxSubmitOptions = {
      // the normal success callback is not used
      // success: function (responseText) {
      //      ...
      // } ,
      complete: function (xhrObj, status) {   
         if(status == "error") { // 500 server error?
            alert("There was an error processing this request.");
         } else {
            if (xhrObj.getResponseHeader("X-My-Custom-Header") != "") {            
               alert("Intercepted special HTTP header...");
               alert(xhrObj.getResponseHeader("X-My-Custom-Header"));
            }
            else {
               // call the function you normally would have used in the "success" callback:
               this._success(xhrObj.responseText);
            }                  
         }
      } ,
      _success: function (responseText) {
         alert("Normal success callback...");
         alert(responseText);
      }
   };

   $('#myForm').ajaxForm(ajaxSubmitOptions);
})

这篇关于jQuery等同于PHP的file_exists()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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