Ajax找不到php文件.为什么?

Ajax cant find php file.. Why?

本文关键字:为什么 文件 php 找不到 Ajax      更新时间:2023-09-26

我正试图创建一个ajax登录表单,但我的jquery脚本找不到php文件。它们在同一个目录中(!),但不起作用。

我的php文件(logreg.php)

<?php
    echo 'true';
?>

我的脚本文件(logreg.js)

$("button[action='"register'"]").click(function(){
    var username = $("#login-username").val();
    var password = $("#login-password").val();
    var repassword = $("#login-repassword").val();
    $.ajax({
        type: "POST",
        url: "logreg.php",
        data: "name="+username+"&pass="+password+"&repass="+repassword,
        success:function(html){
            if(html=='true'){
            }
        },
        beforeSend:function(){
            $("button[action='"register'"]").text("Sendet ...");
        },
        error: function(ts) { $("div#content").html(ts.responseText); }
    });
});

为什么不起作用?我每次都得到"错误404"(找不到)。

它们在同一目录中

URL是相对于HTML文档而不是JavaScript文件的。

您应该能够在浏览器中加载logreg.php,并在屏幕上获得"true"消息。因此,使用任何url访问php文件,然后在js文件中使用相同的url:

url: "http://mydomain.local/logreg.php",