简体   繁体   English

AJAX回调不刷新页面

[英]AJAX callback not refreshing page

Hi guys I have a problem where by I am doing an AJAX callback: 嗨,大家好,我在做AJAX回调时遇到问题:

var htmlToInject;
        function Next(step) {
            var options = {
                cache: false,
                type: "POST",
                url: "Page.aspx?Page=" + step,
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "html",
                success: function (result) {                   
                    htmlToInject = result;                  
                },
                error: function (jqXHR, textStatus, errorThrown) {                   
                },
                complete: function (jqXHR, textStatus) {                  
                    $("#form1").empty();
                    $("#form1").append(htmlToInject);                  
                }
            };
            $.ajax(options);
        }

But the problem lies in where I am using jQuery validation methods and I am creating controls dynamically. 但是问题出在我使用jQuery验证方法以及动态创建控件的地方。 So the callback does not cause a post back so controls would not be generated, which I solved by doing this: 因此,回调不会导致回发,因此不会生成控件,我通过执行以下操作解决了该问题:

                        $("#form1").empty();
                        $("#form1").append(htmlToInject);      

Which then displayed the newly generated controls. 然后显示新生成的控件。

But validation on the fields is giving me errors: 'settings' is null. 但是在字段上进行的验证却给了我错误:“设置”为null。

I took a look at the page source and the source was there for the previously generated controls. 我看了看页面源代码,该源代码位于先前生成的控件中。 I do not want to post back as I do not want the "loading" effect, hence why I am using ajax. 我不想发回邮件,因为我不想使用“加载”效果,因此为什么要使用ajax。

How can I over come this issue guys? 我怎么能克服这个问题?

Many thanks in advance. 提前谢谢了。

If you're using unobtrusive ajax you can do this in the success/complete function as appropriate: 如果您使用的是不显眼的ajax,则可以根据需要在成功/完成功能中执行以下操作:


$('#form1').html(htmlToInject);
$.validator.unobtrusive.parse('#form1');

I took a look at the page source and the source was there for the previously generated controls. 我看了看页面源代码,该源代码位于先前生成的控件中。

If you are talking about Right Click->View Source, than it will show HTML you originaly get from server when page is loaded with regular POST/GET (some browsers may even repeat request to server to get it). 如果您谈论的是Right Click-> View Source,那么它将在页面加载常规POST / GET时显示从服务器原始获取的HTML(某些浏览器甚至可能重复请求服务器获取它)。 If you want to see changes done by JS/AJAX - you need to use developer tools. 如果要查看JS / AJAX所做的更改,则需要使用开发人员工具。 All modern browsers have that. 所有现代浏览器都具有该功能。 FireFox may need Firebug to be installed. FireFox可能需要安装Firebug。 Just press F12 to see those tools. 只需按F12键即可查看这些工具。

But validation on the fields is giving me errors: 'settings' is null. 但是在字段上进行的验证却给了我错误:“设置”为null。

I do not see any validation initialization code after you change form content: 更改表单内容后,我看不到任何验证初始化代码:

complete: function (jqXHR, textStatus) {                  
                    $("#form1").empty();
                    $("#form1").append(htmlToInject);                  
                }

You should add $("#form1").validate({...}) (or how your validation plugin is initialized) again after you have placed new form elements. 放置新的表单元素后,应再次添加$("#form1").validate({...}) (或如何初始化验证插件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM