[英]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.