如何让我的JS与服务器上的php对话?

我正在尝试从我的本地机器向一个存放php文件的网站发出Ajax请求。站点中现在只有一个文件——php,当文件在同一个文件夹中时,它可以正常工作,但是当url指向我的web 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>');
    }


}