简体   繁体   English

代码可在localhost上运行,但不能在实时服务器上运行

[英]code works on localhost but not live server

My code works perfectly fine on localhost. 我的代码在localhost上运行良好。 However, when I copied and pasted the exact same code on my live server I got this error message: Failed to load resource: the server responded with a status of 404 (). 但是,当我在实时服务器上复制并粘贴完全相同的代码时,出现以下错误消息:无法加载资源:服务器响应状态为404()。 I checked my folder path and it looks correct to me. 我检查了我的文件夹路径,它对我来说看起来正确。 I don't know what is wrong here. 我不知道这是怎么回事。

var folder = "image/";
$(document).ready(function () {
    $.ajax({
        url: folder,
        success: function (data) {
         console.log("successful load")
            $(data).find("a").attr("href", function (i, val) {
                if (val.match(/\.(jpe?g|png|gif)$/)) {
                      theImageList.push(folder + val );
            };
        })
            left_show_image(theImageList[shuffledComparison[0][0]],window.innerWidth/10*4.88, screen.width/3, "left")
            right_show_image(theImageList[shuffledComparison[0][1]],window.innerWidth/10*4.88, screen.width/3, "right")\
        },
        error: function(){
            alert("cannot read your folder")
        }
    })
})

This is really confusing to me and I would appreciate any advice. 这真的让我感到困惑,我将不胜感激。

You need to fix the folder path. 您需要修复文件夹路径。 Try adding the slash before image: 尝试在图像前添加斜杠:

var folder = "/image/";

OR 要么

var folder = "~/image/";

Also place this inside of the document.ready function 还要将其放在document.ready函数中

$(document).ready(function () {
 var folder = "/image/";
    $.ajax({
        url: folder,
        success: function (data) {
        console.log("successful load")
        $(data).find("a").attr("href", function (i, val) {
            if (val.match(/\.(jpe?g|png|gif)$/)) {
                  theImageList.push(folder + val );
        };
    })
        left_show_image(theImageList[shuffledComparison[0]
[0]],window.innerWidth/10*4.88, screen.width/3, "left")
        right_show_image(theImageList[shuffledComparison[0]
[1]],window.innerWidth/10*4.88, screen.width/3, "right")\
    },
    error: function(){
        alert("cannot read your folder")
    }
}) 
})

This will use the base url and then search for the image folder so it should work for both local host and on the server. 这将使用基本url,然后搜索image文件夹,因此它对于本地主机和服务器均适用。 Also, make sure you have uploaded your image folder to the server 另外,请确保已将图像文件夹上载到服务器

Using relative paths in javascipt may cause 404 error when deploying web site under a virtual or root directory. 在虚拟目录或根目录下部署网站时,在javascipt中使用相对路径可能会导致404错误。

image folder should work with both virtual or root directory. 映像文件夹应与虚拟或根目录一起使用。 you provide a variable for application path such as appUrl. 您为应用程序路径(例如appUrl)提供了一个变量。

var appUrl = 'localhost or live server root folder' var appUrl ='本地主机或实时服务器根文件夹'

var folder = appUrl + "/image"; var folder = appUrl +“ / image”;

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

相关问题 在实时服务器上未定义,但在本地主机上工作 - Undefined on live server, but WORKS on localhost AJAX 适用于本地主机,但不适用于实时服务器 - AJAX works on localhost, but doesn't on live server jQuery ajax调用在本地主机上有效,但在实时服务器上无效 - jquery ajax call works on localhost, but not on live server JavaScript代码可在localhost上运行,但不能在实时站点上运行? - JavaScript code works in localhost but not in live site? Live Server 工作正常,但本地主机出现 404 错误 - Live Server works but I get 404 error for localhost Laravel sanctum 在本地主机上工作,但在实时服务器中返回 401 unauthenticated - Laravel sanctum works in localhost, but returns 401 unauthenticated in live server JavaScript可在Localhost上使用,但不能在实时站点上使用 - JavaScript works on Localhost, but not on live site 代码使用“Live Server”工作,但在通过“GitHub Pages”部署时不能工作 - Code works using "Live Server" but not when deployed via "GitHub Pages" Favicon 可以在本地主机上运行,​​但不能在服务器上运行? - Favicon works on localhost but not on a server? 在视图中声明和使用PHP变量可以在Localhost上正常运行,但不能在实时服务器(ubuntu AWS)Laravel 4.1上运行 - Declaring and using PHP variables in views works fine in Localhost but not on live server (ubuntu AWS) Laravel 4.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM