Jquery ajax检查文件存在但仍然出错 [英] Jquery ajax check file exists but still error

查看:80
本文介绍了Jquery ajax检查文件存在但仍然出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery ajax检查服务器上是否存在任何类型的文件



这是我用来从数据库表中获取文件路径的代码

  function  getDocumentPath(documentId){
var param = ' Ref_DocID:' + documentId
$ .ajax({
类型: POST
url: ../ MyAppWService.asmx + / + GetDocumentPath
data : { + param + }
contentType: application / json; charset = utf-8
dataType: json
成功:documentPathSuccess,
错误:documentPathError
});
}





这个是我用来检查文件是否存在的代码

  function  documentPathSuccess(response){
// $(#documentText)。html(response.d);
var fileUrl = response.d;
var filename = fileUrl.substring(fileUrl.lastIndexOf ( \\)+ 1 );
fileUrl = http:// myserver:portnumber / apppath / docs / + filename;

$ .ajax({
type: 获取
url:fileUrl,
成功: function (){
$( a#doclink)。prop ( href,fileUrl); // 这是锚链接,点击此链接可以下载该文件
$( #documentpreview)。prop( src ,fileUrl); // 这是显示pdf文件的iframe
},
错误: function (){
$( #doclink)。prop( href );
}
})
}





我面临的问题是我可以得到来自服务器的任何类型的文件。

当检查所获取的文件路径是否存在时,即使文件存在于服务器上,也会调用ajax错误函数。

当文件类型为PDF时,我不会遇到此问题,但仅适用于doc,docx,ppt等办公室文档。



请告知我需要在我的代码中做出哪些更改



提前谢谢



我是什么尝试过:



我用jquery ajax来检查服务器上是否存在文件,但仍然会调用错误函数来获取ajax请求。

解决方案

.ajax({
type: POST
url: ../ MyAppWService.asmx + / + GetDocumentPath
data: { + param + }
conten tType: application / json; charset = utf-8
dataType: json
成功:documentPathSuccess,
错误:documentPathError
});
}





这个是我用来检查文件是否存在的代码

  function  documentPathSuccess(response){
//


(#documentText)。html(回复。 d);
var fileUrl = response.d;
var filename = fileUrl.substring(fileUrl.lastIndexOf( \\)+ 1 );
fileUrl = http:// myserver :portnumber / apppath / docs / + filename;


.ajax({
type: GET
url: fileUrl,
成功: function (){


I am using jquery ajax to check if a file of any type exist on the server

This is the code i use for to get file path from database table

function getDocumentPath(documentId) {
  var param = 'Ref_DocID : ' + documentId
  $.ajax({
  type: "POST",
  url: "../MyAppWService.asmx" + "/" + "GetDocumentPath",
  data: "{" + param + "}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: documentPathSuccess,
  error: documentPathError
  });
}



This is the code that i use to check if the file exists

function documentPathSuccess(response) {
  //$("#documentText").html(response.d);                
  var fileUrl = response.d;
  var filename = fileUrl.substring(fileUrl.lastIndexOf("\\") + 1);
  fileUrl = "http://myserver:portnumber/apppath/docs/" + filename;
  
  $.ajax({
   type: "GET",
   url: fileUrl,
   success: function () {
      $("a#doclink").prop("href", fileUrl);//this is the anchor link by clicking this the file is made available for download
      $("#documentpreview").prop("src",fileUrl);//this is the iframe where pdf files get displayed
  },
  error: function () {
      $("a#doclink").prop("href", "#");
  }
 })
}



The issue that I am facing is that I can get any type of file from the server.
What happens is that while checking if the fetched filepath exists the ajax error function is called even if the file exists on the server.
I dont face this issue when the filetype is PDF but only for office docs like doc,docx,ppt etc.

Please advise on what changes do i need to make in my code

Thanks in advance

What I have tried:

I have used jquery ajax to check if file exists on server but still error function gets called for ajax request.

解决方案

.ajax({ type: "POST", url: "../MyAppWService.asmx" + "/" + "GetDocumentPath", data: "{" + param + "}", contentType: "application/json; charset=utf-8", dataType: "json", success: documentPathSuccess, error: documentPathError }); }



This is the code that i use to check if the file exists

function documentPathSuccess(response) {
  //


("#documentText").html(response.d); var fileUrl = response.d; var filename = fileUrl.substring(fileUrl.lastIndexOf("\\") + 1); fileUrl = "http://myserver:portnumber/apppath/docs/" + filename;


.ajax({ type: "GET", url: fileUrl, success: function () {


这篇关于Jquery ajax检查文件存在但仍然出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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