JQuery:无法使用相对路径访问php文件
对于Ajax请求,很难获得相对路径 从like.js开始,我尝试访问likeunlike.php 错误消息: jquery-3.3.1.js:9600 POST 404(未找到) 文件结构: JQuery:JQuery:无法使用相对路径访问php文件,php,jquery,ajax,relative-path,Php,Jquery,Ajax,Relative Path,对于Ajax请求,很难获得相对路径 从like.js开始,我尝试访问likeunlike.php 错误消息: jquery-3.3.1.js:9600 POST 404(未找到) 文件结构: JQuery: $(document).ready(function(){ // like and unlike click $(".content").on("click",".like",function(){ var id = $(this).attr("id");
$(document).ready(function(){
// like and unlike click
$(".content").on("click",".like",function(){
var id = $(this).attr("id"); // Getting Button id
var split_id = id.split("_");
var postid = split_id[1];
var userid = split_id[2];
// AJAX Request
$.ajax({
url: '../serverside/likeunlike.php',
type: 'post',
data: {postid:postid,userid:userid},
dataType: 'json',
success: function(data){
var likes = data['likes'];
var type = data['type'];
$("#likes_" + postid + "_" + userid).text(likes);
if(type == 1){
$("#like_" + postid + "_" + userid).css("color","lightseagreen");
}
if(type == 0){
$("#like_" + postid + "_" + userid).css("color","#ffa449");
}
}
});
});
});
已按照其中一个答案的要求提供索引文件。希望能有帮助。
Index.php:
<?php
include "detail/config.php";
?>
<html>
<head>
<title>Talk</title>
<link href="style/style.css" type="text/css" rel="stylesheet" />
<script src="jquery/jquery-3.3.1.js" type="text/javascript"></script>
<script src="search/script/like.js" type="text/javascript"></script>
<script src="search/check/check.js" type="text/javascript"></script>
</head>
<script>
$(function() {
$('form').on("submit", function(e) {
e.preventDefault();
$('#error').text(""); // reset
var name = $.trim($("#search").val());
if (name.match(/[^a-zA-Z0-9 ]/g)) {
$('#error').text('Please enter letters and spaces only');
return false;
}
if (name === '') {
$('#error').text('Please enter some text');
return false;
}
if (name.length > 0 && name.length < 3) {
$('#error').text('Please enter more letters');
return false;
}
$.ajax({
url: 'search/search.php',
method: 'POST',
data: {
msg: name
},
dataType: 'json',
success: function(response) {
$(".content").html("")
$(".total").html("")
if(response){
var total = response.length;
$('.total') .append(total + " Results");
}
$.each(response, function() {
$.each($(this), function(i, item) {
var mycss = (item.Type == 1) ? ' style="color: #ffa449;"' : '';
$('.content').append('<div class="post"><div class="post-text"> ' + item.MessageText + ' </div><div class="post-action"><input type="button" value="Like" id="like_' + item.ID + '_' + item.UserID + '" class="like" ' + mycss + ' /><span id="likes_' + item.ID + '_' + item.UserID + '">' + item.cntLikes + '</span></div></div>');
});
});
}
});
});
});
</script>
<body>
<form action="index.php" method="post" id="myForm" autocomplete="on"><pre>
<input name="msg" id="search" type="text" autofocus value= "<?php if(isset($_POST['msg'])) {
echo htmlentities ($_POST['msg']); }?>"></input> <span id="error"></span>
<input type="submit" style="border:0; padding:0; font-size:0">
</pre></form>
<div class="total">
</div>
<div class="content">
</div>
</body>
</html>
谈话
$(函数(){
$('form')。关于(“提交”,函数(e){
e、 预防默认值();
$(“#错误”).text(“”;//重置
变量名称=$.trim($(“#搜索”).val();
如果(名称匹配(/[^a-zA-Z0-9]/g)){
$(“#错误”).text('请仅输入字母和空格');
返回false;
}
如果(名称==''){
$(“#错误”).text('请输入一些文本');
返回false;
}
如果(name.length>0&&name.length<3){
$(“#错误”).text('请输入更多字母');
返回false;
}
$.ajax({
url:'search/search.php',
方法:“POST”,
数据:{
msg:name
},
数据类型:“json”,
成功:功能(响应){
$(“.content”).html(“”)
$(“.total”).html(“”)
如果(答复){
var total=响应长度;
$('.total')。追加(total+“Results”);
}
$.each(响应,函数(){
$。每个($(本),功能(i,项){
var mycss=(item.Type==1)?'style=“color:#ffa449;”:“”;
$('.content').append(''+item.MessageText+''+item.cntLikes+'');
});
});
}
});
});
});
jquery/js没有他的位置信息
这应该是工作
我已通过以下方式解决了这一问题:
使我的目录结构和文件名更简单。它被过度设计了。我现在有我的根文件夹,然后只有一个级别低于文件夹
我一直在挣扎的Ajax url路径被修改为“serverside/like.php”,它获取了php文件,但什么也没发生
在查看like.php代码时,我有一个include(“../con/config.php”),没有“;”。我加了这个,现在效果很好
谢谢大家的帮助。非常感谢,一如既往
新文件夹结构:
您的所有页面都在main index.php上加载,对吗?url:“../serverside/likeunlike.php”@vivekmodi hi抱歉。这是我第一次尝试将代码分成不同的文件夹。php是主页。我不太明白这个问题。我的知识不是很渊博。对不起,我不知道你的意思。你能详细说明一下吗。我正在努力解决这个问题。我认为您不需要使用../只需在URL中使用serverside/likeunlike.php谢谢。这会导致错误jquery-3.3.1.js:9600 POST 404(未找到)。我认为,您的php文件只能通过index.php访问。你能显示你的index.php吗?有任何地方引用likeunlike.php吗?好的,谢谢。在问题中添加了index.php文件代码。如果没有完整的代码,很难解决此问题。除了index.php或likeunlike.php之外,还有.htaccess文件吗?在index.php文件中还有一个ajax调用。这个调用有效吗?是的,Ajax调用有效,并且没有.htacces文件。我不知道那是什么。
var base_url = window.location.origin;
// like and unlike click
$(".content").on("click",".like",function(){
var id = $(this).attr("id"); // Getting Button id
var split_id = id.split("_");
var postid = split_id[1];
var userid = split_id[2];
// AJAX Request
$.ajax({
url: base_url + '/search/serverside/likeunlike.php',
type: 'post',
data: {postid:postid,userid:userid},
dataType: 'json',
success: function(data){
var likes = data['likes'];
var type = data['type'];
$("#likes_" + postid + "_" + userid).text(likes);
if(type == 1){
$("#like_" + postid + "_" + userid).css("color","lightseagreen");
}
if(type == 0){
$("#like_" + postid + "_" + userid).css("color","#ffa449");
}
}
});
});
});