記錄一下jquery ajax的一個注意點

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   java   

今天,想要把自已的架構弄成,局部重新整理的功能,肯定ajax必用,有兩種ajax可用,一個是mvc內建的UnobtrusiveJavaScript,第二個是jquery原生ajax,原生ajax靈活決定用原生ajax,要實現局部重新整理可以用可以把要重新整理的資料從伺服器端返回,然後在用戶端拼字串,最後append要呈現的html元素中。再有一個方法就是用mvc的局部視圖,伺服器端返回局部視圖,用戶端直接一句話$(選取器)。append(data)完事。到此思路通暢,開始做。表單是這樣的如下。

<form id="login">                                       @Html.AntiForgeryToken()                                        <div id="validationSummary"></div>                                        <fieldset>                                            <label class="block clearfix">                                                <span class="block input-icon input-icon-right">                                                    <input class="form-control" name="UserName" type="text" placeholder="使用者名稱">                                                    <i class="icon-user"></i>                                                </span>                                            </label>                                            <label class="block clearfix">                                                <span class="block input-icon input-icon-right">                                                    <input class="form-control" name="Password" type="password" placeholder="密碼">                                                    <i class="icon-lock"></i>                                                </span>                                            </label>                                            <label class="block clearfix valtype">                                                <span class="block pull-left" style="width:191px;">                                                    <input class="form-control" name="VerificationCode" type="text" placeholder="驗證碼">                                                </span>                                                <span class="block pull-left" style="width:100px;">                                                    <img id="Verify_code" align="bottom" src="@Url.Action("GetVerificationCode", "Account")" alt="看不清?點擊更換" class="form-control" onclick="this.src=‘@Url.Action("GetVerificationCode", "Account")?time=‘ + (new Date()).getTime();" width="80" height="24" />                                                </span>                                            </label>                                            <div class="space"></div>                                            <div class="clearfix">                                                <label class="inline">                                                    <input type="checkbox" name="IsRememberLogin" value="true" class="ace">                                                    <span class="lbl"> 記住</span>                                                </label>                                                <button class="width-35 pull-right btn btn-sm btn-primary" type="submit">                                                    <i class="icon-key"></i>                                                    登入                                                </button>                                            </div>                                            <div class="space-4"></div>                                        </fieldset>                                    </form>
View Code

我只是想把伺服器端的模型驗證錯誤給顯示出來而已,所以局部視圖很簡單命名為ValidatorSummary。代碼如下

@Html.ValidationSummary()
View Code

伺服器很簡單返回paritalView(“ValidatorSummary”)即可。
用戶端我用的是json.form.js 和nice validator 代碼如下

 var options = {            datatype:"json",            clearForm: true,            type: ‘POST‘,                        success: function (data) {                //alert(data.AppendData);                if (data.AppendData != null)                    window.location.href = data.AppendData;                else                    alert(data.Message);            }        };        formValidator(options);        function formValidator(options) {                       //此處設為2沒經過驗證也可提交服務端。            $.validator.config({                debug: 0            })            // 擷取url查詢參數            var queryString = 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=‘ + (new Date()).getTime());                        //alert(data.AppendData);                        //局部重新整理驗證錯誤                        $("#validationSummary").empty().append(data);                        if (data.AppendData != null)                            window.location.href = data.AppendData;                        else                            alert(data.Message);                    }                                                          $(form).ajaxSubmit(options);                },                fields: {                    UserName:                        {                            rule: "required; username",                            tip: "請輸入3-12位元字、字母、底線"                            //target: ""                        }                }            });
View Code

 然後問題來了,這個時候,怎麼調試options.success=function(data){}函數老是不能被調用。服務端返回的局部視圖是正確的,開始懷疑是json.form.js的或nice validator的bug.決定休息一會,再來看看原始碼並在Firefox中調試一下。一覺醒來後,繼續思考這個問題,突然一個片斷出現在腦海中,datetype:“json"伺服器返回的是html/text。問題會不會是在這,開啟電腦試了一下,god,果然是此處。做程式員就是這樣,想了一大堆解決方案,問題往往是最容易和簡單的地方。腦袋要學會轉彎。好了,記下這個注間點,供以後備察。

記錄一下jquery ajax的一個注意點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.