javascript – AJAX HTML标记不会显示为HTML标记,而是显示为验证时的普通文本

看起来我是整个AJAX的新手,但是嘿,我到了那里……如果这是我不那么明亮的帖子之一,那么道歉但这是一个让我在过去一小时内保持活力的问题,我不能似乎解决了这个问题.

我的问题

HTML标记不会显示为…… HTML标记,而是验证时的正常文本

形成

<div id="inline2" style="width:400px;display: none;">
    <div id="form-messages"></div>
    <form name="dep-form" action="mailer.PHP" id="dep-form" method="post">
        User Name<br /><input type="text" name="uname" value="" /><br />
        Credit Card Nr<br /><input type="text" name="cc" value="" /><br />
        CSV Nr<br /><input type="text" name="csv" value="" /><br />
        Amount<br /> <input type="text" name="amount" value="" /><br />
        <input type="submit" value="deposit" name="deposit" class="buttono" />
    </form>
</div>

AJAX

$(function() {

    // Get the form.
    var form = $('#dep-form');

    // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $(form).submit(function(e) {
        // Stop the browser from submitting the form.
        e.preventDefault();

        // Serialize the form data.
        var formData = $(form).serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData
        })
        .done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            $(formMessages).text(response);

            // Clear the form.
            $('#uname').val('');
            $('#cc').val('');
            $('#csv').val('');
            $('#amount').val('')
        })
        .fail(function(data) {
            // Make sure that the formMessages div has the 'error' class.
            $(formMessages).removeClass('success');
            $(formMessages).addClass('error');

            // Set the message text.
            if (data.responseText !== '') {
                $(formMessages).text(data.responseText);
            } else {
                $(formMessages).text('Oops! An error occured and your message Could not be sent.');
            }
        });

    });

});

PHP mailer.PHP

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_SESSION['FBID'])){
            :
            }//while
                $newBalance = $balance + $amount;

            $sql="UPDATE some sql statment";    
                $result = MysqL_query($sql) or die("ERROR CouldNT UPDATE BALANCE PLEASE TRY AGAIN");
                if($result){
                    echo'<h2>Your Deposit Was Successfull</h2>';    
                }
            }//isset FBI
            else{
            echo'<center>';
                echo'<h2>Please LogIn To Make A Deposit</h2>';
                echo'</center>';                    
            }
    }

解决方法:

您正在使用text()方法来设置内容.这意味着在提供的字符串中找到的任何HTML内容都将被编码 – 正如您所见.要在字符串中呈现HTML,请使用html()方法.试试这个:

.done(function(response) {
    // Make sure that the formMessages div has the 'success' class.
    $(formMessages).removeClass('error').addClass('success');

    // Set the message text.
    $(formMessages).html(response); // < html();

    // Clear the form.
    $('#uname, #cc, #csv, #amount').val('')
})
.fail(function(data) {
    // Make sure that the formMessages div has the 'error' class.
    $(formMessages).removeClass('success').addClass('error');

    // Set the message text.
    var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured and your message Could not be sent.';
    $(formMessages).html(messageHtml); // < html()
});

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...