使PHP的值显示在请求AJAX数据的页面上


Making value from PHP show on page requesting AJAX data

我有一个简单的登录表单,它确实在函数中返回"成功"文本,但现在我希望能够添加.php文件中提供的文本。我该怎么做呢?

html

 <script>
  $(function(){
      $("#submitlogin").click(function() {

        inputs = {
            "logInUsername" : $('input[name=logInUsername]').val(),
            "logInPassword" : $('input[name=logInPassword]').val()
        };
        // since this is a username and password combo you will probably want to use $.post
        $.ajax ({
            type: "POST",           
            url: "loggnow.php",
            data: inputs,
            success: function() {
                $("#login").html("You are now logged in!");
            },
            error : function(jqXHR, textStatus, errorThrown){
                alert("error " + textStatus + ": " + errorThrown);
            }
        });
      });
      });
    $.ajax({
        type: "POST",           
        url: "loggnow.php",
        data: inputs,
        dataType: "html",
        success: function (data) {
            $("#login").html(data);
        },
        error : function (jqXHR, textStatus, errorThrown) {
            alert("error " + textStatus + ": " + errorThrown);
        }
    });

jquery网站上的文档说success函数将传递数据。(见报道)。所以你可以直接使用"data"变量传递回来的内容,并把它放在那里。

success(data, textStatus, jqXHR)
A function to be called if the request succeeds. The function gets passed 
three arguments: The data returned from the server, formatted according to 
the dataType parameter; a string describing the status; and the jqXHR 
(in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success 
setting can accept an array of functions. Each function will be called in 
turn.