使用jquery ajax在服务器上的文件夹上存在检查文件

时间:2022-09-16 23:45:44

I am trying to check file exist on server using ajax. i have used below script my server name is like www.Mydomain.netp/Application_Folder/

我试图使用ajax检查服务器上是否存在文件。我使用下面的脚本我的服务器名称就像www.Mydomain.netp / Application_Folder /

 var fileobj="../invoices/"+filename+".pdf";

  var pdfurl;
     $.ajax({
      url: fileobj, //or your url
    success: function(data){

     alert('exists');
   pdfurl = "http://Mydomain.orgi/Application_Folder/Invoices/" + Invoiceid + ".pdf";

         window.open(pdfurl, "PopupWindow", "width=600,height=600,scrollbars=yes,resizable=no");

     },
      error: function(data){



            alert('does not exists');

               pdfurl = "http://AnotherDomain.orgi/Invoices/" + Invoiceid + ".pdf";
                 window.open(pdfurl, "PopupWindow", "width=600,height=600,scrollbars=yes,resizable=no");
   },
 });

If file exists that time also i am getting into error part, any other alternative ways to do this. above script perfect with localhost but not working on production environment

如果文件存在那段时间我也会进入错误部分,任何其他替代方法来执行此操作。上面的脚本与localhost完美但不适用于生产环境

1 个解决方案

#1


The code snippet you provided seems to use calls from two different domains "Mydomain.orgi" and "AnotherDomain.orgi". You need to check if the another server you are requesting follows CORS. I will suggest console/debug your code And another minor thing that a variable named Invoiceid has been used. Please check if this also resolves well.

您提供的代码段似乎使用来自两个不同域“Mydomain.orgi”和“AnotherDomain.orgi”的调用。您需要检查您请求的另一台服务器是否遵循CORS。我将建议控制/调试您的代码以及另一个使用名为Invoiceid的变量的小事。请检查这是否也能很好地解决。

As long as the file existence is to be checked and as you have already included jquery on your page, I would just try the following

只要要检查文件是否存在,并且您已经在页面上包含了jquery,我就会尝试以下操作

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

or if I follow the promise pattern I would go for the following:

或者,如果我遵循承诺模式,我会采取以下措施:

var jqxhr = $.get( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });

For more here

在这里更多

#1


The code snippet you provided seems to use calls from two different domains "Mydomain.orgi" and "AnotherDomain.orgi". You need to check if the another server you are requesting follows CORS. I will suggest console/debug your code And another minor thing that a variable named Invoiceid has been used. Please check if this also resolves well.

您提供的代码段似乎使用来自两个不同域“Mydomain.orgi”和“AnotherDomain.orgi”的调用。您需要检查您请求的另一台服务器是否遵循CORS。我将建议控制/调试您的代码以及另一个使用名为Invoiceid的变量的小事。请检查这是否也能很好地解决。

As long as the file existence is to be checked and as you have already included jquery on your page, I would just try the following

只要要检查文件是否存在,并且您已经在页面上包含了jquery,我就会尝试以下操作

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

or if I follow the promise pattern I would go for the following:

或者,如果我遵循承诺模式,我会采取以下措施:

var jqxhr = $.get( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });

For more here

在这里更多