简体   繁体   English

Jquery ajax给我一个404未找到的错误

[英]Jquery ajax is giving me a 404 not found error

I have the following code: 我有以下代码:

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
    function loadPhotos(folderName){
            var folder = "assets/photos/"+folderName+"/";
            $.ajax({
                url : folder,
                success: function (data) {
                    $(data).find("a").attr("href", function (i, val) {
                        if( val.match(/\.(jpe?g|png|gif)$/) ) { 
                            $("body").append( "<img src='"+ folder + val +"'>" );
                        } 
                    });
                }
            });
          }
</script>
</head>

<html>
 <div onclick="loadPhotos('7thAnnual')">7th Annual</div>
</html>

For some odd reason when I click on the div, the loadPhotos function throws a 404 not found error on the ajax call.... but the directory that it is saying it can't find, does exist. 由于某些奇怪的原因,当我点击div时,loadPhotos函数在ajax调用上抛出404未找到的错误....但是它说它无法找到的目录确实存在。

For the record I am running this on localhost ( http://127.0.0.1:8020/ ). 为了记录,我在localhost( http://127.0.0.1:8020/ )上运行它。

the directory structure is Ljf/assets/photos/7thAnnual .... 目录结构是Ljf / assets / photos / 7thAnnual ....

so the full path would be http://127.0.0.1:8020/Ljf/assets/photos/7thAnnual 所以完整的路径将是http://127.0.0.1:8020/Ljf/assets/photos/7thAnnual

the 7thAnnual directory holds all the images 7thAnnual目录包含所有图像

Any thoughts? 有什么想法吗?

Okay.... so it seems that I have to fully qualify the name in the url like so: 好的....所以我似乎必须完全限定网址中的名称,如下所示:

var folder = "127.0.0.1:8020/Ljf/assets/photos/7thAnnual";

instead of using just: 而不是仅使用:

var folder = "Ljf/assets/photos/7thAnnual"

This answer brings a 这个答案带来了一个

"Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource." “只有协议方案才支持交叉源请求:http,数据,chrome,chrome-extension,https,chrome-extension-resource。”

which is a different issue than this post, but it does solve the 这是一个与这篇文章不同的问题,但确实解决了这个问题

404 not found 404未找到

So I'll mark it as the answer 所以我会把它标记为答案

You must create the directory assets/photos/7thAnnual/. 您必须创建目录assets / photos / 7thAnnual /。 Be sure you did that 一定要你这样做

If you run this in chrome and open the nifty little inspector 如果你在chrome中运行它并打开漂亮的小检查员

(right click on the background somewhere, choose inspect, then go to the network tab on the inspection window) (右键单击某处的背景,选择检查,然后转到检查窗口的网络选项卡)

then click the div that should trigger the ajax, it should show you the path it tries to get to. 然后单击应该触发ajax的div,它应该显示它尝试到达的路径。 (on your little network panel at the very bottom) (在你最小的网络面板上)

Make sure that the path looks right 确保路径看起来正确

Look at the response to see if there is any other potentially useful info 查看响应以查看是否还有其他可能有用的信息

let me know what you find! 让我知道你发现了什么!

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

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