Write a note about jquery Ajax

Source: Internet
Author: User

Today, want to make their own framework, local refresh function, definitely Ajax must use, there are two kinds of Ajax available, one is the unobtrusivejavascript of MVC, the second is the jquery native Ajax, the original Ajax flexible decision with native Ajax, To implement a partial refresh can be used to return the data to be refreshed from the server side, and then in the client to misspell the string, and finally append the HTML element to render. Another way is to use the partial view of MVC, the server side back to the local view, the client directly a word $ (selector). Append (data) is finished. To this idea unobstructed, began to do. The form is as follows.

<form id="Login">@Html. AntiForgeryToken ()<div id="ValidationSummary"></div> <fieldset> <la Belclass="Block Clearfix"> <spanclass="Block Input-icon input-icon-right"> <inputclass="Form-control"Name="UserName"Type="text"Placeholder="User name"> <iclass="Icon-user"></i> </span> &lt ;/label> <labelclass="Block Clearfix"> <spanclass="Block Input-icon input-icon-right"> <inputclass="Form-control"Name="Password"Type="Password"Placeholder="Password"> <iclass="Icon-lock"></i> </span> &lt ;/label> <labelclass="Block Clearfix valtype"> <spanclass="Block Pull-left"style="width:191px;"> <inputclass="Form-control"Name="Verificationcode"Type="text"Placeholder="Verification Code"> </span> <spanclass="Block Pull-left"style="width:100px;"> "Verify_code"align="Bottom"Src="@Url. Action ("Getverificationcode", "Account")"alt="can't see? Click Replace" class="Form-control"onclick="this.src= ' @Url. Action ("Getverificationcode", "Account")? time= ' + (new Date ()). GetTime ();"Width=" the"height=" -"/> </span> &LT;/LABEL&G                                            T <divclass="Space"></div> <divclass="Clearfix"> <labelclass="inline"> <input type="checkbox"Name="Isrememberlogin"Value="true" class="Ace"> <spanclass="LBL"> Remember </span> </label> <buttonclass="width-35 pull-right btn btn-sm btn-primary"Type="Submit"> <iclass="Icon-key"></i>Login</button> </div> <divclass="space-4"></div> </fieldset> </form>
View Code

I just want to show the server-side model validation errors, so the partial view is simply named Validatorsummary. The code is as follows

@Html. ValidationSummary ()
View Code

The server simply returns Paritalview ("Validatorsummary").
Client I'm using the Json.form.js and Nice validator code below

 varOptions ={datatype:"JSON", ClearForm:true, type:'POST', Success:function (data) {//alert (data. AppendData);                if(Data. AppendData! =NULL) Window.location.href=data.                AppendData; Elsealert (data.            Message);        }        };        Formvalidator (options); function Formvalidator (options) {//This set to 2 is not validated and can be submitted to the service side. $.validator.config ({debug:0            })            //get URL query parameters            varQueryString =Location.search; //alert (queryString);$("#login"). Validator ({theme:"Simple_bottom", Valid:function (form) {//$.post ("/account/index/login", $ (form). Serialize (), function (data) {$ ("#summary"). Empty (). append (data);})Options.url ="/account/index/login"+queryString; Options.success=function (data) {$ ("#Verify_code"). attr ("src",'@Url. Action ("Getverificationcode", "account") Time='+ (NewDate ()). GetTime ()); //alert (data.                        AppendData); //Local Refresh validation error$("#validationSummary"). Empty (). append (data); if(Data. AppendData! =NULL) Window.location.href=data.                        AppendData; Elsealert (data.                    Message);                } $ (form). Ajaxsubmit (options); }, Fields: {UserName: {rule:"required; Username", Tip:"Please enter a 3-12-digit number, letter, underline"                            //target: ""                        }                }            });
View Code

Then the question comes, at this time, how to debug the Options.success=function (data) {} function is always unable to be called. The local view returned by the server is correct and begins to suspect a bug that is json.form.js or nice validator. Decide to take a break, and then look at the source code and debug it in Firefox. Wake up, continue to think about this problem, suddenly a fragment appeared in the Mind, Datetype: "JSON" server returned is Html/text. The question is whether it is here, open the computer to try, God, sure enough is here. As programmers do, and think of a lot of solutions, the problem is often the easiest and easiest place. Head to learn to turn. All right, make a note of this point, for backup.

Write a note about jquery Ajax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.