Javascript ajax中的变量声明

Javascript ajax中的变量声明,javascript,jquery,ajax,Javascript,Jquery,Ajax,嗨,我需要编写一个ajax函数来重新加载我的免费标记工具页面的div中的内容 Div包含一个带有yes或no单选按钮的问题,当用户选择yes并单击submit按钮时,页面应该重新加载。由于我对ajax非常陌生,因此我编写了类似的内容,请告诉我在创建变量时编写的格式是否正确,并请告诉我是否编写了语法错误,谢谢 <script type="text/javascript"> function submitOptIn() { $('optInError').hide();

嗨,我需要编写一个ajax函数来重新加载我的免费标记工具页面的div中的内容

Div包含一个带有yes或no单选按钮的问题,当用户选择yes并单击submit按钮时,页面应该重新加载。由于我对ajax非常陌生,因此我编写了类似的内容,请告诉我在创建变量时编写的格式是否正确,并请告诉我是否编写了语法错误,谢谢

<script type="text/javascript">
function submitOptIn() {
    $('optInError').hide();
    dataString = $('#partnerOptIn').serialize();
    $.ajax({
        data: dataString,
        timeout: 30000,
        type: "POST",
        var newHtml = "<h4>Thank you</h4>
        <p>We appreciate your time to respond to our request.</p>";
        success: function(html){
            $('#optInContent').html(newHtml);
        },
        success: function(html){
            $('#optInContent').html(html);
        }, 
        error: function(){
            $('#optInError').show();
        }
    });
}
</script>

函数submitOptIn(){
$('optInError').hide();
dataString=$('#partnerOptIn')。序列化();
$.ajax({
数据:dataString,
超时:30000,
类型:“POST”,
var newHtml=“谢谢
感谢您抽出时间回复我们的请求。

“; 成功:函数(html){ $('#optInContent').html(newHtml); }, 成功:函数(html){ $('#optInContent').html(html); }, 错误:函数(){ $('#optInError').show(); } }); }
将newHTML声明移动到success函数的内部:

    success: function(html){
            var newHtml = "<h4>Thank you</h4><p>We appreciate your time to respond to our request.</p>";
            $('#optInContent').html(newHtml);
    },
成功:函数(html){
var newHtml=“谢谢我们感谢您抽出时间回复我们的请求。

”; $('#optInContent').html(newHtml); },
首先,您应该在
success
函数中声明
newHtml
变量,而不是在它之前:

success: function(html){
    var newHtml = "<h4>Thank you</h4><p>We appreciate your time to respond to our request.</p>";
    $('#optInContent').html(newHtml);
},
成功:函数(html){
var newHtml=“谢谢我们感谢您抽出时间回复我们的请求。

”; $('#optInContent').html(newHtml); },
该死,你比我快了2秒。