我正在尝试从我的本地计算机向一个包含我的php文件的网站发出Ajax请求。现在网站上只有一个文件。当文件位于同一文件夹中时它工作正常,但当url指向我的网址时它不会做任何事情。如何让我的JS与服务器上的php通信。另外,我知道这个代码很糟糕。这是我正在使用的一个例子。
$('#btn').click(function(){
//pull vars
var username = $('#username').val();
var password = $('#password').val();
//request for username
$.post("Main URL", {user:username,pass:password}).done(function(data){
$("#loginMessage").html(data);
});
});
托管服务器上的php端
if($_SERVER["REQUEST_METHOD"] == "POST")
{
//assign vars after formatting to avoid a wrong login with correct
//username and password
$username = trim($_POST["user"]);
$password = trim($_POST["pass"]);
//specfic message
$errorMessage = "The username ".$username." or the password ".$password. " is wrong";
$welcomeMessage = "Welcome ".$username;
//user name or pass word is wrong invovke failure html
if($password != "1234" || $username != 'Adin')
{
include("html.html");
echo('<message>'.$errorMessage.'</message>');
}
//username and password is right invoke the html with proper messages
if($password == "1234" && $username == "Adin")
{
include("html.html");
echo('<message>'.$welcomeMessage.'</message>');
}
}