简体   繁体   English

在实时域中部署时,ajax调用不起作用

[英]ajax call not working when deployed in live domain

My ajax call is working on localhost but not when i upload the files in domain. 我的ajax调用正在localhost上工作,但在我上传域中的文件时却没有。 Using ajax I am searching all jpg/png files in a folder called 'images' and showing them in my webpage. 使用ajax我正在搜索名为“images”的文件夹中的所有jpg / png文件,并在我的网页中显示它们。 The code is - 代码是 -

<script>

//Use ajax to load all images (jpe?g|png|gif) from a folder to a page called Gallery
//images folder should be in the same folder as the file
      var folder = "../images/";
      $.ajax({
          url : folder,
          success: function (data) {
              $(data).find("a").attr("href", function (i, val) {
                  if( val.match(/\.(jpe?g|png|gif)$/) ) {

// create 'img' element using JS and dynamically add image source and class
                      var imgSrc = document.createElement('img');
                      imgSrc.src= folder + val;
                      imgSrc.className = 'imageThumbnails';
                      $("#spanImage").append(imgSrc);
                  }
              });
          }
      });
</script>

please change 请更换

var folder = "../images/";

to

var folder = "images/";

hope this helps.. cheers 希望这有助于...干杯

Ok, so as you saw when you tried accessing the folder directly in your browser, your web server does not allow this, which is common on web servers. 好的,正如您在浏览器中直接访问文件夹时所看到的那样,您的Web服务器不允许这样做,这在Web服务器上很常见。 Very few people actually want visitors to be able to see a list of all files in a folder, for security reasons. 出于安全原因,很少有人真正希望访问者能够看到文件夹中所有文件的列表。

The quick and dirty way to do this would be to allow listing files in that folder, through a htaccess file, using Options +Indexes , but I strongly recommend you do not do that 快速而肮脏的方法是允许通过htaccess文件使用Options +Indexes列出该文件夹中的文件, 但我强烈建议你不要这样做

Instead, I would suggest you place a file inside your images folder called index.php and have that file build you a list of files placed alongside it, in the images folder. 相反,我建议你在一个名为index.php的图像文件夹中放置一个文件,并让文件在images文件夹中为你创建一个文件列表。 That way you have control over which files you show and which ones you don't. 这样,您可以控制显示哪些文件以及哪些文件不显示。 The index.php file can return a simple text output, one file name per line or something like that. index.php文件可以返回一个简单的文本输出,每行一个文件名或类似的东西。 Then your ajax call should work as it used to. 然后你的ajax调用应该像以前一样工作。

Hope this helps! 希望这可以帮助!

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

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